mhpreiman
10/17/2017 - 5:46 PM

arrow function syntax

() => indicates that a variable stores a function, eg:

//Long syntax
myFunction = () => {
  //code..
};


//Shorthand (must be one-liner!)         - returns the output without 'return' keyword 
myFunction = () => /*code*/;             //without parameter
myFunction = myParameter => /*code*/;    //w parameter; concise body 
myFunction = (myParameter) => { /*code*/ };    //w parameter; block body

Shorthand example:

getString = () => "tere";               //returns tere

getNumber = aNumber => aNumber * 2;     //getNumber(15) returns 30

Foreach:

//Usual foreach loop
groceries.forEach(function(groceryItem) {
  console.log(groceryItem);
});

//Shorthand
groceries.foreach(groceryItem => console.log(groceryItem));