Arrow functions
Arrow functions are a shorter way to write function expressions using the => syntax. Beyond saving keystrokes, they behave differently from regular functions in one important way: they do not have their own this, so they use the this of the surrounding code. That makes them ideal for callbacks, array methods and small helpers, and a poor fit for object methods and constructors.
From function expression to arrow
// Regular function expression
const square = function (n) {
return n * n;
};
// Arrow with a block body
const squareA = (n) => {
return n * n;
};
// Arrow with an implicit return (expression body)
const squareB = (n) => n * n;
// No parameters, or several parameters, need parentheses
const now = () => Date.now();
const add = (a, b) => a + b;
// Returning an object literal needs wrapping parentheses
const makeUser = (name) => ({ name, active: true });
const prices = [120, 80, 250];
const withTax = prices.map((p) => p * 1.18);
With a single parameter you may drop the parentheses (n => n * n), though many style guides keep them for consistency.
How this works inside an arrow
A regular function gets its this from how it is called. An arrow function captures this from where it is defined, and nothing can change it, not even call, apply or bind. This solves an old problem with callbacks inside methods.
class Timer {
constructor() {
this.seconds = 0;
}
start() {
// Arrow keeps "this" pointing at the Timer instance
setInterval(() => {
this.seconds += 1;
console.log(this.seconds);
}, 1000);
}
}
new Timer().start();
If the callback were a regular function, this inside it would not be the timer, and this.seconds would be NaN or throw in strict mode. Older code solved this with const self = this or .bind(this).
Other differences from regular functions
- No
argumentsobject. Use rest parameters instead:(...args) => args.length. - Cannot be used with
new; callingnewon an arrow throws a TypeError. - No
prototypeproperty. - Not hoisted like function declarations; an arrow stored in
constcannot be used before that line runs.
Where arrows cause bugs
The classic mistake is using an arrow as an object method. Suppose an object user has a name property and a method written as greetArrow: () => `Hi ${this.name}`. Calling user.greetArrow() does not print the name, because the arrow took this from the code around the object literal, not from user. Writing the method with shorthand syntax, greet() { return `Hi ${this.name}`; }, fixes it, since a normal method gets this from the object before the dot.
The same problem appears with DOM event listeners where you expect this to be the clicked element. Inside an arrow listener, use event.currentTarget instead.
A second slip is the object-literal return. () => { name: 'x' } returns undefined, because the braces are read as a function body and name: as a label. Wrap the object in parentheses.
When to choose which
| Use an arrow for | Use a regular function or method for |
|---|---|
Array callbacks like map and filter | Object and class methods that use this |
| Timers and promise handlers inside methods | Constructors |
| Short one-line helpers | Functions that need arguments or hoisting |
Readability is the last consideration. Very long arrow chains, such as an arrow returning an arrow returning an arrow, are valid but hard to scan. If a callback grows beyond a few lines, give it a name with a regular declaration or a named const so stack traces and code reviews are clearer.
What to remember
Arrows are concise and take this from their surroundings. Use them for callbacks, wrap returned objects in parentheses, and avoid them for methods and constructors.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.