Arrow Function in JavaScript: Syntax & this

Easy100% Free~15 mins#arrow-functions#this#lexical-this#es6#strict-mode
Interview Answer (30-Second Summary)

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';
2
3const 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};
16
17console.log(user.regularLater());
18console.log(user.arrowLater());
19
20const double = (n) => n * 2;
21console.log([1, 2, 3].map(double).join(' '));
Predict Console Output
Interactive Challenge

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.

Call Stack (Top = Active)
Global
user.regularLater()
Lexical Scope / Bindings
this:user
Console Stream
> [empty]

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.

  1. No own this: a regular function gets this from the call: user.method() sets it to user, while a plain call inner() sets it to undefined in strict mode. An arrow function never gets its own this; it reads the this of the code around it, the same way it reads any outer variable. That is why inner in arrowLater sees user.
  2. No own arguments, no prototype, no new: arguments inside an arrow refers to the enclosing regular function's arguments, ArrowFn.prototype is undefined, and new ArrowFn() throws TypeError: ... is not a constructor. call, apply and bind cannot change an arrow's this either.
  3. Where each fits: use arrows for callbacks inside methods (setTimeout, map, Promise handlers), where keeping the outer this is exactly what you want. Use regular functions or method shorthand for object methods, constructors, prototype methods and event handlers that need this to 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.

Q1.
What is an arrow function in JavaScript?

A compact function expression written with =>, introduced in ES6. With a single expression body it returns that value without the return keyword, as in (n) => n * 2.

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.

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().

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.

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.

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.

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