Skip to solution
mediumFrontend

Explain 'this' keyword in JavaScript.

1.1k views
01

Understand the problem

Question presented to candidate: "In your own words, what is this in JavaScript, and why do people find it confusing compared to other languages?"

What a strong answer should cover:

  • this is not decided by where a function is defined -- it is decided by how the function is called (the call-site), which makes it fundamentally different from ordinary JavaScript variable lookups.
  • Ordinary variables in JavaScript use lexical scoping: you can tell what a variable refers to just by reading where the code is written. this breaks that pattern -- the exact same function body can produce a different this every single time it is called, depending on the call-site.
  • Arrow functions are the deliberate exception: they do not have their own this at all, and instead capture this lexically from the enclosing scope, which re-aligns this with the rest of the language's normal scoping rules.
  • A single shared function reference, called through different objects (or with none at all), genuinely produces a different this each time -- this is not a special case, it is the general rule.
  • The confusion with other languages usually comes from assuming this behaves like Python's explicit self parameter or Java's this, both of which are fixed to the enclosing class instance and cannot be reassigned by how you call a method.

Clarifying questions expected:

  • "Should I focus on the conceptual model here, or would you rather I walk through the exact binding-rule priority order?" (the precise rules have their own dedicated question)

Code / implementation expected: Yes -- a short, runnable snippet showing one function reference producing three different this values depending on how it is called.

thiscontextfunctionsscope
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interview questions. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed output is

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSOne function, three call-sites, three different this values (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 48 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track