Fundamentalsbeginner
Strict Mode Explained
The opt-in stricter variant of JavaScript that catches silent errors, disables risky features, and is the default inside ES modules and classes.
Strict mode is a stricter variant of the language that turns silent failures into errors.
Sloppy mode is like a lenient proofreader who silently 'fixes' your typos without telling you; strict mode is an editor who stops and flags every mistake immediately so you actually learn about it instead of shipping it.
Key Concepts
1
'use strict'; // at the top of a file or a function2
It is the default in ES modules and inside classes. Anything in an import/export file, and any class
body, is already strict. The directive is only needed in scripts and in non-module code.
importexport
3
What it changes, and why each matters.Undeclared assignment throws.
4
x = 5; // sloppy: creates a global. strict: ReferenceError5
A typo in a variable name used to create a new global and continue. Now it fails at the line that is wrong.
6
Assigning to a read-only property throws rather than being ignored:
7
const o = Object.freeze({ a: 1 });
o.a = 2; // sloppy: silently nothing. strict: TypeError8
this is undefined in a plain function call.
thisundefined
9
function f() { console.log(this); }
f(); // sloppy: the global object. strict: undefined10
This is the one that changes behaviour most visibly, and it is an improvement — a method called without its
object now fails instead of quietly writing to globals.
11
Duplicate parameter names are a syntax error, and octal literals like 0755 are rejected.
0755
12
delete on a variable throws rather than returning false.
delete
13
Why it exists rather than just being the language. Backwards compatibility. Making these changes
unconditionally would have broken the web, so they were made opt-in — and then made the default in the two
places that were new enough to afford it.
14
The practical position. Write modules and you are in strict mode without doing anything. In older
scripts, add the directive at the top of the file rather than per function.
15
One caution when adding it to existing code. A file that relied on sloppy behaviour — an implicit global,
a this that defaulted to the window object — will break. That is the point, and it is still worth doing
per file rather than all at once.
this
16
What the interviewer is probing.1. "What does strict mode change?" Probing: the specifics. Stalls: "It is stricter." *Moves
up:* assigning to an undeclared variable throws, this is undefined rather than the global in plain
functions, duplicate parameters are errors, and silent failures become thrown ones.
17
2. "Where is strict mode automatic?" Probing: where it already applies. Stalls: "Only if you
add the directive." Moves up: ES modules and class bodies are always strict, so most modern code
already runs under it.
18
3. "Why does strict mode help performance?" Probing: the engine argument. Stalls: "It does
not." Moves up: it removes dynamic behaviours like with, letting the engine make stronger
assumptions.
19
4. "What breaks when you enable it on old code?" Probing: migration risk. Stalls: "Nothing."
Moves up: accidental globals and functions relying on this defaulting to the global object.