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:
thisin a plain function call — verified directly: calling a plain (non-method) function in sloppy mode givesthisthe global-ish object; in strict mode,thisis genuinelyundefined— a real, observable difference that prevents accidentally mutating global state through a mis-boundthis. - 📌 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.