Strict Mode
What 'use strict' changes — silent errors become throws, this is undefined, and how modules enable strict by default.
- javascript
- strict-mode
Strict mode is a restricted JS variant that turns whole classes of silent mistakes into errors and fixes some legacy footguns. You opt in with 'use strict' — or you already have it in ES modules and class bodies.
'use strict';
// Assignment to undeclared variable → ReferenceError
// x = 1;
function f() {
return this; // undefined in strict (not globalThis) for bare calls
}
f();
How to enable
// Entire script
'use strict';
// Per function (legacy pattern)
function legacy() {
'use strict';
// ...
}
// module.js — automatically strict
export function g() {}
Bundled app code as ESM is strict. Non-module classic scripts without the pragma are sloppy mode.
Behavioral highlights
| Topic | Sloppy | Strict |
|---|---|---|
| Assign undeclared | creates global | ReferenceError |
this bare call |
global object | undefined |
| Duplicate params | allowed | SyntaxError |
with |
allowed | SyntaxError |
Octal 0123 |
legacy octal | SyntaxError (use 0o) |
| Writing read-only prop | silent fail | TypeError |
delete unqualified id |
ok-ish | SyntaxError |
'use strict';
const obj = {};
Object.defineProperty(obj, 'x', { value: 1, writable: false });
// obj.x = 2; // TypeError
Why this matters for interviews
const obj = {
n: 1,
getN() { return this.n; },
};
const loose = obj.getN;
loose(); // strict: this undefined → throw on .n; sloppy: global mess
Strict mode made the “lost this” bug loud. Combine with arrows / bind awareness.
Arguments and callee
Strict mode forbids arguments.callee and makes arguments less magical (no dynamic link to named params in the old way). Prefer rest parameters.
React “Strict Mode” is different
React’s <StrictMode> double-invokes certain lifecycles in development to surface impure effects. Unrelated to 'use strict', same English words — clarify in interviews.
Should you still write the pragma?
- Modules: no need.
- Old non-module scripts: yes if you maintain them.
- New code: prefer modules; tooling assumes strict.
Interview answer (out loud)
“Strict mode makes silent errors throw — undeclared assignments, bad deletes, immutable writes — and sets this to undefined on bare calls. ES modules and classes are strict by default. React StrictMode is a separate development-only purity check.”
Eval and strict
function sloppy() {
eval('var leaked = 1');
}
// strict eval has its own scope — less leakage
function strict() {
'use strict';
eval('var notLeaked = 1');
}
Prefer never eval. Strict mode still reduces the damage surface.
Classes and modules recap
class C {
m() {
// strict even without pragma
}
}
You rarely write 'use strict' in 2026 app code because the module boundary already enabled it. Know the differences for maintaining webpack-bundled IIFE leftovers.
Further reading
Related
Related guides
- AbortControllerCancel fetch and other async work with AbortController and AbortSignal — timeouts, race conditions, and cleanup when components unmount.
- Array find, some, every, includesShort-circuiting array predicates: find, findIndex, some, every, and includes — when to use each and common interview traps.
- Array map, filter, reducemap, filter, and reduce as the core transform toolkit — immutability, chaining costs, reduce patterns, and when a plain loop is clearer.
- Arrow Functions Deep DiveLexical this, no arguments object, no construct, concise bodies — when arrows help and when methods and generators need classic functions.
- Async Iteration and for await...ofAsync iterables, for await...of, and streaming data — how async iterators differ from Promise.all and when to use each.