Skip to the solution
MediumFrontend6 min read

What does 'use strict' do?

228 views

The problem

Question presented to candidate: "What does adding 'use strict' at the top of a file actually change about how the code runs? Give me two or three concrete behavior differences, not just 'it makes errors stricter.'"

What a strong answer should cover:

  • πŸ“Œ Interview term: 'use strict' β€” a directive (a special string literal, not a comment) that opts a script or function into a stricter variant of JavaScript's semantics, turning several silent failures/quirky behaviors into real, thrown errors.
  • πŸ“Œ Interview term: undeclared-variable throw β€” verified directly: in sloppy mode, assigning to a variable that was never declared silently creates a global variable; in strict mode, the identical assignment throws a real ReferenceError.
  • πŸ“Œ Interview term: this in a plain function call β€” verified directly: calling a plain (non-method) function in sloppy mode gives this the global-ish object; in strict mode, this is genuinely undefined β€” a real, observable difference that prevents accidentally mutating global state through a mis-bound this.
  • πŸ“Œ Interview term: silent-failure-to-throw conversion β€” verified directly: assigning to a property of a frozen object silently no-ops in sloppy mode (no error, and the value just doesn't change); in strict mode, the identical assignment genuinely throws a real TypeError.
  • A precise answer names that classes and ES modules are implicitly strict β€” no directive needed, verified directly β€” while plain <script> tags and CommonJS modules are sloppy by default unless the directive is explicitly added.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; naming 2-3 concrete, verified behavior differences (not a vague "it's safer") is the strong signal.

Code / implementation expected: Yes β€” demonstrating at least one sloppy-vs-strict contrast directly (undeclared globals or the frozen-object throw) proves real, not memorized, understanding.

  • strict-mode

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

6 min read Β· 8 sections

Run the code

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

JavaScriptReal sloppy-vs-strict-mode behavior differences: undeclared globals, this, and frozen objects

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 107 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 nextMedium

What does Promise.withResolvers() solve and how does it replace the deferred anti-pattern?

Continue