JavaScript Strict Mode Use Strict Explained
Learn what JavaScript strict mode is, how to enable it with 'use strict', and how it catches silent mistakes that would otherwise go unnoticed.
JavaScript strict mode is a stricter way to parse and run JavaScript. It turns several silent mistakes into errors and changes a few confusing language behaviors.
You enable it in classic scripts with a directive string: "use strict".
How to Enable Strict Mode
Put the directive at the top of a script or function body, before any other statement. That placement matters because directive strings only count before normal code starts.
"use strict";
let score = 100;
console.log(score);Comments can appear above the directive. A real statement cannot, because then the string is no longer treated as a directive.
You can also enable it for one function:
function calculateTotal(items) {
"use strict";
return items.length;
}Only that function body is strict. Other functions are strict only if their own code is strict.
What Strict Mode Catches
Strict mode is useful because it makes common mistakes fail loudly.
"use strict";
score = 100;This throws a ReferenceError because score was never declared. In non-strict scripts, that mistake can create a global variable.
Strict mode also catches writes to read-only properties:
"use strict";
const user = {};
Object.defineProperty(user, "id", { value: 1, writable: false });
user.id = 2;The assignment throws a TypeError instead of failing silently.
Strict Mode Changes this
In a plain function call, strict mode leaves this as undefined.
function showThis() {
"use strict";
console.log(this);
}
showThis();The output is undefined. In non-strict browser scripts, a plain function call can use the global object instead, which often causes accidental global mutations.
Where Strict Mode Is Automatic
Modern JavaScript turns on strict mode automatically in some places:
| Code Location | Strict by Default |
|---|---|
| JavaScript module | Yes |
| Class body | Yes |
| Classic script file | No |
| Function body | Only with directive |
If you are writing modules, you do not need to add "use strict". If you are writing an older classic script, adding the directive is still useful.
Strict Mode Scope
Strict mode is lexical. That means it depends on where code is written, not who calls it.
"use strict";
function strictFunction() {
return "strict";
}This function is strict because it is written inside a strict script. But a separate non-strict function does not become strict just because strict code calls it.
There is also no directive that turns strict mode off inside a strict scope.
Should You Use Strict Mode?
For new classic script files, yes. It catches real mistakes and makes behavior easier to reason about.
For modules and classes, it is already active. For old legacy code, enable it carefully and test, because strict mode can expose mistakes that were previously ignored.
For broader consistency, see the JavaScript code style guide. You can also practice small strict-mode examples in Chrome DevTools.
Rune AI
Key Insights
- JavaScript strict mode is enabled with a use strict directive.
- The directive must appear before other statements in a script or function body.
- Modules and class bodies are strict automatically.
- Strict mode turns several silent failures into errors.
- Strictness is lexical, so calling a non-strict function from strict code does not make that function strict.
Frequently Asked Questions
Do modules need use strict?
Can strict mode be disabled inside strict code?
Does strict mode catch every bug?
Conclusion
Strict mode makes JavaScript less forgiving in useful ways. Use it in classic scripts, and remember that modules and class bodies already run in strict mode.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.