ES6 Arrow Function

 


   An arrow function expression or syntax is a simplified as well as a more compact  version of a regular or normal function expression or syntax.


Syntax: 

   For Single Argument:  let function_name = argument1 => expression

   For Multiple Arguments: let function_name = (argument1, argument2 , ...) => expression

  

Limitations of Arrow Functions:-


An arrow function expression is an alternative to a traditional function expression, but there are some limitations: 

  • Arrow functions do not have their own bindings to this or super, and should not be used as methods.
  • It is not suitable for the call, apply and bind methods, which generally rely on establishing a scope.
  • Arrow functions cannot be used as constructors.

##### Note:
Arrow functions are the best choice when working with closures or callbacks, but it is not a good choice when working with object methods or constructors.


// ARROW FUNCTIONS

// Creating a regular function
// const harry = function (){
//     console.log("This is the best person ever")
// }

// Converting it to an arrow function
// const harry = ()=>{
//     console.log("This is the best person ever")
// }
// harry();

// function returning something
// const greet = function(){
//     return "Good Morning";
// }

// One liners do not require braces/return
// one line will automatically return
// const greet = () =>  "Good Morning";

// const greet = () =>  ({name: "harry"});

// Single parameters do not need parenthesis
// but you will have to put parenthesis if there are multiple paramteres
const greet = name =>  "Good Morning " + name + ending;



console.log(greet('Harry'))

Comments