Arrow function (=>)
Creates a concise function expression that lexically binds this.
Syntax
(params) => expression
(params) => { statements }
Returns
function — A new function object.
Examples
const add = (a, b) => a + b;
console.log(add(2, 3));
Output
5
const nums = [1, 2, 3];
console.log(nums.map(n => n * n));
Output
[ 1, 4, 9 ]
const makeCounter = () => {
let n = 0;
return () => ++n;
};
const c = makeCounter();
console.log(c(), c());
Output
1 2
Notes
- Arrow functions do not have their own `this`, `arguments`, `super`,
or `new.target`; they inherit `this` lexically.
- They cannot be used as constructors (`new` throws).
- To return an object literal concisely, wrap it in parentheses:
`() => ({ a: 1 })`.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 45 |
| firefox | 22 |
| safari | 10 |
| edge | 12 |
| node | 4.0 |