Full answer
Strict mode was added in ES5 so that old, error-prone behavior could be fixed without breaking existing sites. Placing the string 'use strict' at the top of a script turns it on for that script, and placing it at the top of a function turns it on for that function only. Older engines read the directive as an ordinary string, so it was safe to add.
The main changes are:
- Assigning to an undeclared name throws a
ReferenceErrorinstead of creating a global variable. - Changes that used to fail silently throw a
TypeError: writing to a read-only property, adding a property to a frozen object, or deleting a non-configurable one. - A plain function call gets
thisset toundefinedinstead of the global object. - Duplicate parameter names and some old octal literals such as
010are syntax errors. withstatements are not allowed, andevalcannot add variables to the surrounding scope.argumentsno longer stays linked to the named parameters.- Some future keywords, for example
let,static, andinterface, cannot be used as variable names.
ES modules and class bodies are always strict, and no directive is needed. Because bundlers and frameworks use modules, new code seldom needs the directive, but older scripts and many Node.js CommonJS files still do. A common practice is a lint rule that enforces it where it is not automatic.
function sloppy() {
undeclared = 5; // creates a global variable silently
return typeof undeclared;
}
function strict() {
'use strict';
try {
anotherUndeclared = 5;
} catch (error) {
return error.name;
}
}
console.log(sloppy(), strict());
delete globalThis.undeclared;
function thisInSloppy() {
return this === globalThis;
}
function thisInStrict() {
'use strict';
return this === undefined;
}
console.log(thisInSloppy(), thisInStrict());
function writeToFrozen(strictMode) {
const body = (strictMode ? "'use strict';" : '') + 'const o = Object.freeze({ a: 1 }); o.a = 2; return o.a;';
try {
return new Function(body)();
} catch (error) {
return error.name;
}
}
console.log(writeToFrozen(false), writeToFrozen(true));
for (const [label, source] of [
['duplicate parameter', "'use strict'; function f(a, a) {}"],
['octal literal', "'use strict'; var n = 010;"],
['with statement', "'use strict'; with ({}) {}"],
]) {
try {
new Function(source);
console.log(label, 'allowed');
} catch (error) {
console.log(label, error.name);
}
}
class Example {
method() {
return this;
}
}
const detached = new Example().method;
console.log(detached());Run with Node.js v24.22.1 as js-56.js; two runs gave identical output. Output: number ReferenceError true true 1 TypeError duplicate parameter SyntaxError octal literal SyntaxError with statement SyntaxError undefined
Key points
- Opt in with 'use strict' at the top of a script or function.
- Silent errors become thrown errors: undeclared assignment, read-only writes, and frozen objects.
- Plain function calls get this equal to undefined.
- Modules and classes are strict automatically.
- It removes with, duplicate parameters, and old octal literals.
Common mistakes
- Believing strict mode makes code faster. Its purpose is safety.
- Putting the directive after another statement, where it is just an ordinary string.
- Forgetting that class bodies and modules are already strict.
- Mixing strict and sloppy scripts after concatenation, so the directive covers more or less code than intended.
Follow-ups
- Where must 'use strict' appear for it to work?
- How does strict mode change eval and arguments?
- Why was the directive designed as a string and not a keyword?
Tips
- Give the three changes everyone asks about: undeclared assignment, this in plain calls, and silent failures.
- Note that modules and classes are strict by default; it shows current knowledge.
- Mention a linter rule as the practical way to keep a codebase consistent.