Functions
Arrow functions
Concise function syntax with lexical this.
Arrow functions offer a shorter syntax and, crucially, do not have their own this — they capture it lexically from the enclosing scope.
An arrow function borrows the `this` of the room it was written in, rather than being handed a new one when called.
Key concepts
1
That makes them ideal for callbacks inside methods, where a regular function would lose the this binding. A single-expression arrow has an implicit return; wrap an object literal in parentheses to return it.
implicit returnthis
2
Arrows also have no arguments object and cannot be used as constructors, so reach for a regular function when you need those.
arguments
javascript
const double = n => n * 2; // implicit return
const makeUser = name => ({ name }); // return object literal
const counter = {
count: 0,
start() {
setInterval(() => this.count++, 1000); // arrow keeps this = counter
}
};