"use strict" opts a script or function into a safer dialect that turns silent mistakes into errors. ES modules and class bodies are strict automatically.
Turning it on
"use strict"; // first statement of a script
function f() {
"use strict"; // or of a single function
}You rarely write it today: every ES module (import/export, .mjs, type="module") and every class body is strict whether or not the directive is present. Old-style scripts and CommonJS files are sloppy unless you opt in.
What changes
| Situation | Sloppy mode | Strict mode |
|---|---|---|
| Assign to undeclared variable | Creates a global | ReferenceError |
this in a plain function call | globalThis | undefined |
| Write to read-only / getter-only property | Silently ignored | TypeError |
| Add property to frozen / non-extensible object | Silently ignored | TypeError |
delete a non-configurable property | Returns false | TypeError |
| Duplicate parameter names | Allowed | SyntaxError |
with statement | Allowed | SyntaxError |
Octal literal 010 | 8 | SyntaxError (use 0o10) |
arguments ↔ parameters | Linked (aliasing) | Independent |
eval creates variables in the caller | Yes | No — own scope |
Reserved future words (let, static, implements…) | Usable as names | Reserved |
Seeing both modes side by side
This file is a module (strict). Code built with the Function constructor is sloppy unless it says otherwise — a handy way to compare:
const sloppy = new Function(`
leaked = 1; // becomes a global
const frozen = Object.freeze({ a: 1 });
frozen.a = 2; // silently ignored
return [typeof globalThis.leaked, frozen.a, this === globalThis];
`);
console.log("sloppy:", sloppy());
const strict = new Function(`
"use strict";
try { leaked2 = 1; } catch (e) { return e.name + " / this=" + this; }
`);
console.log("strict:", strict());
function args(a) { a = 99; return arguments[0]; } // module → strict
console.log("arguments unlinked:", args(1));sloppy: [ 'number', 1, true ]
strict: ReferenceError / this=undefined
arguments unlinked: 1What is strict mode and why use it?