Syntax
"use strict";Examples
Catching Accidental Globals
Strict mode throws an error instead of silently creating a global variable.
"use strict";
try {
undeclaredVariable = 5; // ReferenceError in strict mode
} catch (e) {
console.log(e.message);
}
// Without strict mode, this would silently create a global variable - a common bug sourceStrict Mode is Automatic in Modules and Classes
You often get strict mode benefits without writing the directive at all.
// In an ES module file, this is automatic:
export function myFunction() {
// strict mode is already active here
}
class MyClass {
// class bodies are always strict mode, even outside modules
}Best practices
- If writing plain scripts (not modules), add "use strict" at the top of the file or function to catch silent errors
- Remember ES6 modules (using import/export) and class bodies are strict mode by default - no directive needed
- Treat strict mode errors as bugs to fix, not obstacles to work around, since they usually reveal a genuine mistake
- Avoid mixing strict and non-strict code within the same file where possible, for consistent behavior
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification