Skip to the solution
EasyPhone Screen7 min read

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

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.

  • syntax
  • tricky
  • real-world

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptDefault binding under strict vs non-strict mode, including the detached-method bug and primitive auto-boxing (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 5 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

How does Array.prototype.toSorted() differ from .sort(), and why does that matter when updating state immutably?

Continue