Arrow Functions
Arrow functions are shorter, but their real superpower is how they handle this. Knowing when to use them — and when not to — is a common interview question.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Write arrows with zero, one or many parameters
- Use implicit return, including for objects
- Explain lexical this
- Know where arrow functions do not work
Explanation#
Shapes: () => 42, x => x * 2, (a, b) => a + b, and (x) => { const y = x * 2; return y; } with a body.
Without braces, the expression is returned automatically (implicit return). To return an object literal, wrap it in parentheses: () => ({ ok: true }).
Arrows have no own this. They use the this of the surrounding code (lexical this). That makes them perfect for callbacks inside methods — no more var self = this.
Because of that, arrows are a poor choice for object methods and event handlers that need this to be the object/element. They also cannot be used with new and have no arguments object.
Syntax#
const add = (a, b) => a + b;
const makeUser = name => ({ name, active: true });
Code example: Implicit returns and lexical this#
const square = n => n * n;
const makeUser = name => ({ name, active: true });
console.log(square(5));
console.log(makeUser('Ravi'));
const timer = {
seconds: 0,
start() { // regular method: this = timer
[1, 2, 3].forEach(() => {
this.seconds++; // arrow borrows this from start()
});
console.log('seconds:', this.seconds);
},
};
timer.start();
const counter = {
count: 10,
regular() { return this.count; },
};
console.log(counter.regular());
25
{ name: 'Ravi', active: true }
seconds: 3
10Step-by-step explanation#
- square has one parameter, so the parentheses are optional and n * n is returned.
- makeUser wraps the object in ( ) so the braces are not read as a function body.
- Inside start(), this is timer. The arrow callback has no this of its own, so it uses timer too.
- counter.regular is a normal method, so this is counter.
Real-world use case#
array.map(x => …), promise.then(data => …) and React event handlers like onClick={() => setOpen(true)} are all arrow functions.
Common mistakes#
const o = { n: 1, get: () => this.n } returns undefined — the arrow’s this is the outer scope, not o. Use get() { return this.n; }.
() => { ok: true } returns undefined; the braces are a function body with a label. Write () => ({ ok: true }).
const P = () => {}; new P() throws TypeError: P is not a constructor. Use a class.
Best practices#
- Use arrows for callbacks and short helpers
- Use method shorthand for object methods
- Keep implicit-return arrows to one clear expression
- Use classes, not arrows, when you need new
Remember this#
Arrows don’t have their own this — they borrow it from where they were written.
Returning an object? Wrap it: () => ({ … }).
Callback → arrow. Method → shorthand method.
Practice exercise#
Your task: Convert to arrows: function double(n) { return n * 2; } and function toPair(k, v) { return { key: k, value: v }; }
Need a hint?
Show solution
const double = n => n * 2;
const toPair = (key, value) => ({ key, value });
console.log(double(4), toPair('a', 1)); // 8 { key: 'a', value: 1 }
Mini quiz#
What is printed?
const f = () => { value: 1 };
console.log(f());
Want more? Practise all ES6+ MCQs · ES6+ interview questions
My notes#
Saved in this browser.