Arrow Functions
An arrow function is a compact function expression written with =>. It does not have its own this, arguments, or super, and it cannot be used as a constructor.
Detailed Explanation
Arrow functions exist for two reasons: shorter syntax for small callbacks, and lexical this. The second reason is the one that matters.
A function gets this from how it is called. obj.method() binds this to obj. fn() in sloppy mode binds this to the global object. Pass obj.method to setTimeout and this is no longer obj. That loss is the classic bug.
An arrow function does not bind this at all. It reads this from the enclosing scope, the same way it reads any other variable. Write an arrow inside a method, and this inside the arrow is the method's this, even after you pass the arrow around. That is why event handlers and promise callbacks inside classes are often arrows, or are wrapped in arrows.
Syntax variants:
(a, b) => a + b— implicit return of an expression.a => a * 2— parentheses optional for one parameter.(a, b) => { return a + b; }— a block body; you mustreturn.() => ({ x: 1 })— parentheses around an object literal, otherwise{starts a block.
Arrows have no arguments object. Use rest parameters. They have no prototype property and throw if you call them with new. They cannot be generator functions (function* exists; =>* does not). They cannot be used as a super target because they are not constructors and do not have their own this binding to pass along.
They are always expressions. There is no hoisted arrow declaration. const add = (a, b) => a + b is a const in the temporal dead zone until that line runs.
Reach for an arrow when the function is a callback, when you want the surrounding this, or when the body is a short expression. Reach for function when you need this to depend on the caller, when you need a constructor, when you want a hoisted name, or when the function is a method that should be overridable on the prototype.
What it costs you: two silent failures. First, using an arrow as an object method (const obj = { x: 1, get() { /* ok */ }, bad: () => this.x }) makes this the outer scope, often undefined in modules. Second, a block body without return yields undefined, which is easy to miss when you convert (x) => x.n into (x) => { x.n; }.
Code Examples
const nums = [1, 2, 3];
console.log(nums.map((n) => n * 2)); // => [ 2, 4, 6 ]
// Block body needs an explicit return.
console.log(
nums.map((n) => {
return n * 2;
}),
); // => [ 2, 4, 6 ]
// Object literal must be wrapped in parentheses.
const pairs = nums.map((n) => ({ value: n }));
console.log(pairs[0]); // => { value: 1 }const account = {
balance: 10,
// Method: `this` is account when called as account.deposit(...)
deposit(amount) {
const add = () => {
this.balance += amount; // same `this` as deposit
};
add();
},
// Arrow stored as a property: `this` is not account.
broken: () => {
return this;
},
};
account.deposit(5);
console.log(account.balance); // => 15
console.log(account.broken()); // => undefined in modules / strict codeconst Arrow = () => {};
try {
new Arrow();
} catch (err) {
console.log(err.name); // => TypeError
}
console.log(Arrow.prototype); // => undefinedCommon Gotchas
Arrows are not constructors
`new` throws. They also have no `prototype`, so they are the wrong shape for methods you want on a shared prototype.
An arrow method on an object literal does not bind to that object
`this` is lexical, usually the module or function that created the object.
Block bodies do not implicit-return
`(x) => { x + 1; }` returns `undefined`. Use `(x) => x + 1` or write `return`.
No `arguments`
`const f = () => arguments` reads the outer function's `arguments` if one exists, which is almost never what you want. Use `...args`.