Skip to content
JavaScript·Intermediate·Coding·1 min read

How is this determined in JavaScript?

Short interview answer

For ordinary functions, this is determined by the call form: new binding, explicit call/apply/bind, method-style implicit binding, or the default rule. Arrow functions do not create their own this; they resolve it lexically from the surrounding scope.

Example

JavaScript
function whoAmI() { return this; }
whoAmI();                 // undefined (strict) or global (sloppy) — defaultwhoAmI.call({ id: 1 });   // { id: 1 } — explicitconst o = { whoAmI };o.whoAmI();               // o — implicit (method call)new whoAmI();             // the new instance — new binding

Key takeaway

Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.

← Back to The this keyword

Related questions