Introduction & Syntax
Arrow Functions · Study material
Arrow functions ((x) => x * 2) arrived in ES6 and are now the most common way to write callbacks. They are shorter than regular functions, but the real difference is behaviour: an arrow function has no this of its own – it uses the this of the place where it was written – and it has no arguments, cannot be called with new, and ignores call, apply and bind for this. That makes arrows perfect inside methods and terrible as methods – one of the most common interview topics. Then test yourself with 60 practice questions – 15 MCQs, 20 predict-the-output puzzles, 15 debugging questions and 10 coding problems – every one run on Node.js.
Syntax
| Form | Example |
|---|---|
| No parameters | () => "hi" |
| One parameter (parentheses optional) | x => x * 2 or (x) => x * 2 |
| Several parameters, defaults, rest | (a, b = 1, ...rest) => a + b |
| Expression body: implicit return | (a, b) => a + b |
| Block body: explicit return | (a, b) => { const s = a + b; return s; } |
| Returning an object literal | () => ({ ok: true }) |
Without parentheses, () => { ok: true } is a block containing a label – it returns undefined.
const square = (x) => x * x; // one expression: returned automatically
const add = (a, b) => a + b;
const greet = () => "Hello!"; // no parameters: empty parentheses
const toUser = (name, age) => ({ name, age }); // returning an object: wrap it in ( )
const describe = (n) => { // braces = a block body: return is required
const kind = n % 2 === 0 ? "even" : "odd";
return `${n} is ${kind}`;
};
console.log(square(7), add(2, 3), greet(), toUser("Asha", 21), describe(9));
49 5 Hello! { name: 'Asha', age: 21 } 9 is oddLexical this
A regular function's this is decided by how it is called (obj.method(), a plain call, new, call/apply/bind). An arrow function has no this binding at all: inside it, this is looked up in the enclosing scope, exactly like any other variable. So a callback written as an arrow inside a method sees the method's this.
const timer = {
seconds: 3,
regular() {
// a regular callback gets its own this – undefined here, because this example is a (strict) module
return [1].map(function () { return this?.seconds; });
},
arrow() {
// an arrow has no this of its own: it uses the this of arrow(), which is timer
return [1].map(() => this.seconds);
},
};
console.log(timer.regular(), timer.arrow());
[ undefined ] [ 3 ]
Arrows are poor object methods
An arrow written directly as a property of an object literal takes this from the code around the literal (often the module or global scope), not from the object. Use method shorthand (inc() { … }) for methods and arrows for the callbacks inside them.
const counter = {
count: 10,
incRegular() { return ++this.count; }, // this = counter (the object before the dot)
incArrow: () => typeof this, // this comes from where the object literal is written (module top level)
};
console.log(counter.incRegular(), counter.incArrow());
11 undefined
Arrows inside methods: callbacks and timers
The classic pre-ES6 workarounds – var self = this; or .bind(this) – are no longer needed: an arrow callback passed to setTimeout, map, then or an event listener keeps the method's this.
class Countdown {
constructor(from) { this.from = from; }
start() {
const tick = () => { // arrow: this stays the Countdown instance in every call
console.log(this.from);
if (--this.from > 0) setTimeout(tick, 0);
else console.log("Lift off!");
};
tick();
}
}
new Countdown(3).start();
3 2 1 Lift off!
What arrows do not have
Arrow functions have no arguments object (use rest parameters), no prototype property, no new.target, and cannot be used as constructors – new arrow() throws a TypeError. They also cannot be generators.
const arrow = () => 1;
function regular() { return arguments.length; }
console.log(regular(1, 2, 3), typeof arrow.prototype, "prototype" in regular);
try {
new arrow(); // arrows cannot be constructors
} catch (e) {
console.log(e.name);
}
const sum = (...nums) => nums.reduce((a, b) => a + b, 0); // use a rest parameter instead of arguments
console.log(sum(1, 2, 3));
3 undefined true TypeError 6
call, apply and bind cannot change an arrow's this
Because this is fixed when the arrow is created, call, apply and bind can still pass arguments but silently ignore their first (this) argument.
const site = { name: "LearnFrenzy" };
const regular = function () { return this?.name; };
const arrow = () => this?.name; // this is fixed when the arrow is created
console.log(regular.call(site), arrow.call(site), regular.bind(site)(), arrow.bind(site)());
LearnFrenzy undefined LearnFrenzy undefined
Functions that return functions
Arrow syntax keeps curried functions and factories short: a => b => a + b. Each call returns a new arrow that remembers the earlier arguments through a closure.
// Arrows make functions that return functions very short
const multiply = (a) => (b) => (c) => a * b * c;
const applyDiscount = (percent) => (price) => price - (price * percent) / 100;
const festive = applyDiscount(20); // a ready-made function with percent fixed to 20
console.log(multiply(2)(3)(4), [500, 1200].map(festive));
24 [ 400, 960 ]
Common mistakes
- Arrow as an object or prototype method – this is not the object.
- Braces without return – (x) => { x * 2 } returns undefined.
- Returning an object without parentheses – () => { a: 1 } returns undefined.
- Expecting bind to fix an arrow – it has no effect on this.
- Using arguments inside an arrow – you get the outer function's arguments (or a ReferenceError).
- new with an arrow – TypeError: not a constructor.
- Thinking arrows fix var in loops – scoping still depends on var vs let.
Key points to remember
- Expression body returns automatically; block body needs return.
- Wrap returned object literals in parentheses.
- this inside an arrow = this of the enclosing scope (lexical).
- No arguments, prototype, new.target, constructor use or generator form.
- call/apply/bind cannot change an arrow's this.
- Use arrows for callbacks; use regular functions or method shorthand for methods.
- Class-field arrows give each instance a bound handler.
Frequently asked questions
Should I always use arrow functions?
Use them for callbacks, short helpers and functions that should keep the outer this. Use method shorthand for object and class methods, function declarations when hoisting helps readability, and regular functions when you need this, arguments or new.
Are arrow functions faster?
No meaningful difference. Choose them for clarity and their this behaviour, not speed.
Why do React components often use arrow functions?
Handlers written as arrows (inline or as class fields) keep this or close over props and state without manual bind calls.
Where can I learn more?
Read JavaScript Functions and The this Keyword in the JavaScript tutorial, then practise with the exercises below.