Arrow Function in JavaScript: Syntax & this
An arrow function in JavaScript is a shorter function syntax, (a, b) => a + b, that does not have its own this, arguments or prototype: it uses the this of the code around it. That makes arrows ideal for callbacks inside methods, and wrong for object methods, constructors and anything that needs its own this.
Key Learning Objectives
Write arrow functions with concise bodies, implicit returns and parameter shorthand.
Explain lexical this: why an arrow function uses the this of the surrounding code.
Predict this inside a regular inner function versus an arrow inner function in strict mode.
Know where arrow functions do not fit: methods, constructors, prototypes and arguments.
The Interview Problem
What do the three console.log calls print, and why do the regular and the arrow inner functions see a different this?
1'use strict';23const user = {4 name: 'Ada',5 regularLater() {6 const inner = function () {7 return this;8 };9 return inner();10 },11 arrowLater() {12 const inner = () => this.name;13 return inner();14 },15};1617console.log(user.regularLater());18console.log(user.arrowLater());1920const double = (n) => n * 2;21console.log([1, 2, 3].map(double).join(' '));
Predict Console Output
Select the option that matches what this code prints to the console:
undefined / Ada / 2 4 6
Ada / Ada / 2 4 6
undefined / undefined / 2 4 6
undefined / Ada / undefined undefined undefined
V8 Engine Execution Trace
Step 1 of 6 (Line 17)regularLater is called as a method, so its own this is user.
Arrow Function in JavaScript Explained
Arrow Functions and Lexical this
An arrow function is a shorter function expression: (n) => n * 2. With an expression body it returns the value automatically; with a block body { ... } it needs return like any other function.
- No own
this: a regular function getsthisfrom the call:user.method()sets it touser, while a plain callinner()sets it toundefinedin strict mode. An arrow function never gets its ownthis; it reads thethisof the code around it, the same way it reads any outer variable. That is whyinnerinarrowLaterseesuser. - No own
arguments, noprototype, nonew:argumentsinside an arrow refers to the enclosing regular function's arguments,ArrowFn.prototypeisundefined, andnew ArrowFn()throwsTypeError: ... is not a constructor.call,applyandbindcannot change an arrow'sthiseither. - Where each fits: use arrows for callbacks inside methods (
setTimeout,map, Promise handlers), where keeping the outerthisis exactly what you want. Use regular functions or method shorthand for object methods, constructors, prototype methods and event handlers that needthisto be the element.
Common Traps & Mistakes
Writing an object method as an arrow function: method: () => this.name does not see the object, because this comes from the surrounding scope.
Adding braces and forgetting return: (n) => { n * 2 } returns undefined.
Returning an object literal without parentheses: () => { id: 1 } is a block with a label; write () => ({ id: 1 }).
FAANG Follow-Up Probes
Probe #1
How did code keep the outer this in callbacks before arrow functions (const self = this, bind)?
Probe #2
Why do React class components bind handlers or use arrow class fields, and what does each cost?
Probe #3
What does an arrow function at the top level of an ES module see as this?
Arrow Function in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Arrow Functions.
Arrow function vs regular function: what is the difference?
An arrow function has no own this, arguments or prototype, cannot be called with new, and cannot be used as a generator. It takes this and arguments from the surrounding scope. A regular function gets its own this from how it is called.
Why does the arrow function inside the method return 'Ada'?
An arrow function does not create its own this. It looks up this lexically, so inside arrowLater it sees the method's this, which is user because the method was called as user.arrowLater().
Why does the regular inner function return undefined?
inner() is a plain function call with no object before the dot. In strict mode such a call sets this to undefined; in sloppy mode it would be the global object.
Can you change the this of an arrow function with call, apply or bind?
No. call, apply and bind pass the arguments through, but the this argument is ignored because arrow functions always use the this of the scope where they were defined.
When should you not use an arrow function?
For object methods that need this to be the object, for constructors (an arrow function throws 'is not a constructor' with new), for prototype methods, and for DOM event handlers that rely on this being the element.
Do arrow functions have an arguments object?
No. Inside an arrow function, arguments refers to the arguments of the nearest enclosing regular function. Use rest parameters, (...args) => args, to collect an arrow function's own arguments.
More JavaScript interview questions for freshers
