Using the abbreviation “Arrow” instead of the word function. Prior to it, JS had a far less common operator that would be a convenient method to write “goes to,” but it was not as well-known as the arrow function.
function countdown(n) { while (n --> 0) // "n goes to zero" alert(n); }
The ‘n’ variable is decreased to zero by executing this script. It is a simplified notation of the code below.
while( (n--)>0)
The JavaScript ==> function may be used to create the function in a similar manner.
// ES5 var selected = ArrayObj.filter(function (item) { return item.toUpperCase() }); // ES6 var selected = ArrayObj.filter(item=>item.toUpperCase());
The arrow function’s syntax is
(param1, param2, …, paramN) => expression // equivalent to: (param1, param2, …, paramN) => { return expression; } ()==>{return statement;} // with no parameter
This syntax is highly helpful for constructing functions that create filters, maps, or execute any action on an object that is an array, as well as for doing minor adjustments.
// An empty arrow function returns undefined let empty = () => {}; (() => 'foobar')(); // Returns "foobar" var compareVar= a => a > 15 ? 15 : a; compareVar(16); // 15 compareVar(10); // 10 let max = (a, b) => a > b ? a : b; // Easy array filtering, mapping, ... var arr = [5, 6, 13, 0, 1, 18, 23]; var sum = arr.reduce((a, b) => a + b); // 66 var even = arr.filter(v => v % 2 == 0); // [6, 0, 18] var double = arr.map(v => v * 2); // [10, 12, 26, 0, 2, 36, 46]