Arrow Functions
Concise ES6 syntax. Single-param: no parens. Single-expression: no braces/return.
Key Difference
Arrow functions do not have their own this — they inherit it from parent scope.
Concise ES6 syntax. Single-param: no parens. Single-expression: no braces/return.
Arrow functions do not have their own this — they inherit it from parent scope.
const double = n => n*2;
const add = (a,b) => a+b;
const greet = name => {
return `Hi ${name}!`;
};
// Great in array methods:
[1,2,3].map(n=>n*2); // [2,4,6]
[1,2,3].filter(n=>n>1); // [2,3]
More in JavaScript