Full answer
An arrow function is written with =>. With one expression and no braces, the value of the expression is returned automatically. For an object literal, wrap the object in parentheses so the braces are not read as a block.
The shorter syntax is the smallest difference. The important one is this. A regular function gets its own this each time it is called. An arrow function does not create one. It uses the this of the place where it was written, which is why arrows are handy inside methods and callbacks.
Arrow functions also have no arguments object (use rest parameters instead), have no prototype, and throw a TypeError if called with new. They cannot be written as generators.
These limits decide where not to use them. An arrow is a poor choice for an object method that needs this to be the object, and for constructors. Another small point: arrow functions are always anonymous in syntax, but when assigned to a variable the engine gives the function that variable's name, which helps stack traces.
const square = (n) => n * n;
const makeStudent = (name, marks) => ({ name, marks });
console.log(square(7));
console.log(makeStudent('Ira', 88));
const team = {
name: 'Blue',
players: ['Dev', 'Sam'],
listWithArrow() {
return this.players.map((player) => `${player} plays for ${this.name}`);
},
listWithFunction() {
return this.players.map(function (player) {
return `${player} plays for ${this.name}`;
}, { name: 'explicit this' });
},
};
console.log(team.listWithArrow());
console.log(team.listWithFunction());
const sum = (...numbers) => numbers.reduce((total, n) => total + n, 0);
console.log(sum(1, 2, 3, 4));
try {
const Broken = () => {};
new Broken();
} catch (error) {
console.log(error.name + ': ' + error.message);
}Run with Node.js v24.22.1 as js-7.js; two runs gave identical output. Output: 49 { name: 'Ira', marks: 88 } [ 'Dev plays for Blue', 'Sam plays for Blue' ] [ 'Dev plays for explicit this', 'Sam plays for explicit this' ] 10 TypeError: Broken is not a constructor
Key points
- Concise syntax with an implicit return for a single expression.
- No own this; it comes from the enclosing scope.
- No arguments object; use rest parameters.
- Cannot be called with new and have no prototype.
- Not suited as object methods or constructors.
Common mistakes
- Returning an object literal without parentheses, which returns undefined because the braces become a function body.
- Using an arrow as an object method and reading this.name from it.
- Using arguments inside an arrow function and getting the outer function's arguments, or a ReferenceError at the top level.
- Thinking arrow functions are faster. The difference is not a reason to choose one.
Follow-ups
- What is the value of arguments inside an arrow function declared inside a regular function?
- Why can arrow functions not be used as constructors?
- When would you still pick a regular function in modern code?
Tips
- Lead with the this behavior; the syntax is the part everyone already knows.
- Give one case where an arrow is right (callbacks in a method) and one where it is wrong (object methods).
- Mention rest parameters as the replacement for arguments.