Skip to solution
easyPhone Screen

How does 'use strict' change what `this` refers to inside a regular function called without a receiver, compared to non-strict mode?

1.1k views
01

Understand the problem

Question presented to candidate: "If I define a plain function and call it with no receiver -- just fn(), not obj.fn() -- what does this refer to inside it? Does use strict change that, and why would that matter for a real bug, like a detached object method?"

What a strong answer should cover:

  • In non-strict (sloppy) mode, calling a regular function with no receiver sets this to the global object -- globalThis in Node and modern browsers, window historically in browsers.
  • In strict mode, the same bare call leaves this as undefined instead of substituting the global object -- this is called default binding, and strict mode simply skips the substitution step.
  • The classic real bug this explains: extracting a method off an object (const fn = obj.method) and calling it bare loses the receiver. In strict mode this throws when the code tries to use this.something (TypeError: Cannot read properties of undefined); in non-strict mode it silently uses the global object instead, which is arguably worse because it fails silently rather than loudly.
  • Strict mode also skips auto-boxing a primitive passed as this via call/apply -- a primitive stays a primitive in strict mode, but gets wrapped in its object wrapper (Number, String, Boolean) in non-strict mode.
  • ES2015 modules and class bodies are implicitly strict with no pragma needed -- worth knowing this already applies to nearly all modern code without anyone writing use strict by hand.

Clarifying questions expected:

  • "Are we talking about a plain function call, or also arrow functions?" -- arrow functions never have their own this at all, so this specific default-binding rule does not apply to them regardless of strict mode.
  • "Is the use strict pragma placed once per function, or should I assume the whole file or module is strict?" -- placement matters; a pragma only takes effect if it is the literal first statement of the function or file.

Code / implementation expected: Yes -- a real, executed comparison of a bare call in a strict function versus a non-strict function, plus a detached-method example showing the real consequence of each mode.

syntaxtrickyreal-world
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 this-binding 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 — captured output, not i

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSDefault binding under strict vs non-strict mode, including the detached-method bug and primitive auto-boxing (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 5 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track