Strict Mode
Strict mode is a restricted variant of JavaScript that turns silent mistakes into errors and changes a few language rules, most visibly around this, arguments, and undeclared assignments.
Detailed Explanation
JavaScript accumulated sloppy behaviours that the web could not delete. Strict mode is the opt-in cleanup. You enable it with "use strict"; at the top of a file or a function. ES modules and class bodies are strict automatically. CommonJS files in Node are not, unless you add the pragma or the file is treated as ESM.
The changes you actually feel:
Assignment to an undeclared name is a ReferenceError. In sloppy mode x = 1 creates a global. In strict mode it throws. That single rule prevents a class of leak that lasts for the life of the process.
this is undefined in a bare call. fn() in sloppy mode binds this to the global object. In strict mode this is undefined, so this.foo throws instead of writing window.foo. Methods and arrows are unchanged: methods still get the receiver, arrows still use lexical this.
arguments is not mapped. In sloppy mode, changing arguments[0] changes the first parameter and vice versa. In strict mode they are separate. Rest parameters already behave like the strict version.
Duplicates and with-statements are errors. Duplicate parameter names (function f(a, a)) are a syntax error. with is a syntax error. Octal literals like 0644 are a syntax error; use 0o644. Deleting an unqualified name (delete x) is an error. Writing to a read-only property throws instead of failing silently.
eval gets its own scope. A var inside a strict eval does not leak into the caller.
What stays the same: == still coerces. var is still function-scoped. typeof null is still "object". Strict mode is not a type system.
You almost never write "use strict"; in a new app that is already on ES modules. You still need to know it, because a pasted snippet from a classic script, a Node CommonJS helper, or a non-module <script> tag will run sloppy and will accept bugs your modules reject. A library published as CJS may also run sloppy unless it opts in.
What it costs you: a few old patterns break. Code that relied on this === window in a callback, or on implicit globals, throws. That is the point. The migration is to pass values explicitly and to declare every binding.
Reach for modules (strict by default) in new files. Add "use strict"; at the top of any leftover classic script or CommonJS file you still maintain.
Code Examples
"use strict";
try {
oops = 1; // undeclared
} catch (err) {
console.log(err.name); // => ReferenceError
}
function showThis() {
return this;
}
console.log(showThis()); // => undefined"use strict";
function echo(a) {
arguments[0] = 99;
return a;
}
console.log(echo(1)); // => 1 (not mapped)function sloppyThis() {
return this;
}
// In a sloppy non-module script this would be globalThis.
// In an ES module the file is already strict, so it is undefined.
console.log(sloppyThis());Common Gotchas
Modules are already strict
a redundant `"use strict";` does nothing. A CommonJS `.cjs` file is not strict unless you add the pragma.
Bare `this` is `undefined` in strict functions
`this.name` throws. Bind the method, use an arrow, or pass the value as an argument.
Sloppy `x = 1` creates a global
the bug is silent until someone enables strict mode or converts the file to a module.
Classes and `new Function` with a strict body are strict
even if the surrounding script is sloppy.