'use strict' enables strict mode, which blocks several error-prone JavaScript behaviors such as accidental globals and silent failures. Learn function-level vs file-level usage, strict-mode effects on this/eval, and practical migration tips.
What does 'use strict' do in JavaScript?
The core idea
'use strict' turns on strict mode, a safer JavaScript execution mode that converts many silent bugs into explicit errors.
In interviews, a strong answer is: strict mode reduces ambiguity, catches risky patterns early, and makes code behavior more predictable across runtimes.
'use strict';
function run() {
// total = 10;
// ReferenceError: total is not defined
}
run();
Behavior | Sloppy mode | Strict mode |
|---|---|---|
Assigning undeclared variable | Creates accidental global | Throws |
Plain function call | Global object in browser ( |
|
Duplicate function parameters | Often allowed | SyntaxError |
| Allowed (discouraged) | SyntaxError |
Some silent write failures | May fail silently | Throws |
Where to apply it
You can enable strict mode at file scope or function scope:
- File-level: affects the entire script file.
- Function-level: affects only that function body.
Also important: ES modules and class bodies are strict by default, even without writing 'use strict'.
// File-level strict mode
'use strict';
function a() {
return this; // undefined in strict mode
}
// Function-level strict mode
function b() {
'use strict';
return this; // undefined here, even if outer file is non-strict
}
this behavior that surprises people
In strict mode, calling a function without an owning object keeps this as undefined. That prevents accidental writes to global scope and exposes binding bugs earlier.
function show() {
console.log(this);
}
// Non-strict in browser: Window
// Strict: undefined
show();
Common breakpoints during migration
- Implicit globals from missing
let/const. - Legacy code using
with. - Duplicate parameter names in old utility functions.
- Code that expects sloppy-mode
thisfallback to global object. - Mutations that used to fail silently (readonly/non-extensible targets).
'use strict';
const cfg = {};
Object.defineProperty(cfg, 'version', {
value: 1,
writable: false
});
// cfg.version = 2;
// TypeError in strict mode (instead of silent failure in sloppy mode for many cases)
Goal | Recommended practice | Why it helps |
|---|---|---|
Prevent accidental globals | Use strict mode + | Turns hidden runtime leaks into immediate errors. |
Safer modern codebase | Prefer ES modules | Modules are strict by default and have clearer dependency boundaries. |
Lower migration risk | Enable strict mode per file and run tests | Isolates failures and makes fixes incremental. |
Interview one-liner
'use strict' opts JavaScript into a safer mode that rejects legacy error-prone behaviors, making bugs surface earlier and execution semantics more predictable.
Practical scenario
A checkout script accidentally assigns totalAmount = ... without declaration inside a helper. In non-strict mode this leaks a global and causes flaky cross-page behavior; in strict mode it throws immediately so the bug is fixed early.
Common pitfalls
- Adding strict mode and only testing happy paths.
- Assuming old callback code still has global
this. - Mixing legacy scripts and modules without checking runtime boundaries.
Roll out strict mode incrementally by file, and add tests for scope errors,
this binding, and readonly mutation paths.Think of strict mode like turning on compiler warnings-as-errors for runtime behavior: less permissive at first, but much safer for long-term code quality.
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.