JavaScript Arrow Functions
Arrow functions provide a shorter syntax for writing functions: `(a, b) => a + b` instead of `function(a, b) { return a + b; }`. Single expressions are implicitly returned without needing the `return` keyword.
Beyond brevity, arrow functions differ from regular functions in that they don't bind their own `this`, `arguments`, or serve as constructors.
Syntax variations
`(x) => x * 2` for a single expression, `() => {}` for no parameters, and `(a, b) => { return a + b; }` when a full block body is needed.
When to avoid them
Avoid arrow functions for object methods that need `this` to refer to the object, and for functions used as constructors with `new`.
const square = x => x * x;
console.log(square(5));25A single parameter and expression body need no parentheses or braces.
const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2);
console.log(doubled);[2, 4, 6]Arrow functions are especially popular as short callbacks.
Key points
- Arrow functions use `=>` and support implicit returns for single expressions.
- They do not have their own `this` binding.
- They cannot be used as constructors with `new`.
- They're commonly used for concise callbacks in map/filter/reduce.
