Intermediate · 1 of 17 · 14 min read
Strict mode
What "use strict" actually changes, why every module is already strict, and the handful of silent failures it turns into errors.
- Strict mode
Strict mode is a different dialect of JavaScript, selected with a string literal at the top of a file or a function. It was added in ES5 because the language could not fix its early mistakes without breaking the web — so instead of changing the defaults, it offered an opt-in.
Turning it on
'use strict'; // whole file — must be the very first statement
function strictHere() {
'use strict'; // just this function and anything nested in it
// …
}
It must be the first statement, before any other code. A line above it — even a variable declaration — makes it an ordinary, ignored string expression:
const ready = true;
'use strict'; // too late: this is just an expression statement, discarded
undeclared = 1; // no error — the file is still sloppy
console.log(undeclared); // 1
This is the one real gotcha of the directive, and it is silent. Which is a good argument for using modules, where the question does not arise.
What it changes
| Sloppy mode | Strict mode |
|---|---|
| Assigning to an undeclared name creates a global | ReferenceError |
this in a plain call is the global object |
this is undefined |
| Writing to a read-only property fails silently | TypeError |
delete someVariable returns false |
SyntaxError |
| Duplicate parameter names are allowed | SyntaxError |
Octal literals like 010 are allowed |
SyntaxError |
arguments tracks parameter reassignment |
they are decoupled |
Accidental globals become errors
The original sin of the language. A typo used to create a global variable:
'use strict';
function setup() {
conut = 0; // typo for `count`
}
// setup(); // ReferenceError: conut is not defined
Without strict mode, that line silently creates globalThis.conut, the function appears to work, and
the bug surfaces somewhere unrelated.
this is undefined in a plain call
The change with the biggest practical consequence:
'use strict';
function who() {
return this;
}
console.log(who()); // undefined
console.log(who.call('x')); // "x" — and not boxed into a String object
const user = {
name: 'Ana',
greet() { return this.name; },
};
const loose = user.greet;
// console.log(loose()); // TypeError: Cannot read properties of undefined
In sloppy mode, who() returns globalThis and loose() returns undefined instead of throwing —
so a detached method half-works and writes name onto the global object. The strict error is the
better outcome: it points at the real problem, which is that the method lost its receiver.
Strict mode also stops boxing the this value. who.call('x') gives the primitive string in
strict mode and a String wrapper object in sloppy mode.
Silent write failures become TypeError
'use strict';
const frozen = Object.freeze({ a: 1 });
try {
frozen.a = 2;
} catch (error) {
console.log(error.constructor.name); // TypeError
}
const nonWritable = Object.defineProperty({}, 'id', { value: 1, writable: false });
try {
nonWritable.id = 2;
} catch (error) {
console.log(error.constructor.name); // TypeError
}
In sloppy mode both assignments do nothing at all and report nothing — the hardest class of bug to find, because the code reads as if it worked.
The same applies to adding a property to a non-extensible object, or assigning to a getter-only accessor:
'use strict';
const readOnly = { get value() { return 1; } };
try {
readOnly.value = 2;
} catch (error) {
console.log(error.constructor.name); // TypeError — no setter
}
Syntax that is simply rejected
These fail at parse time, so the file does not run at all:
'use strict';
// function dupes(a, a) {} // SyntaxError: duplicate parameter name
// const legacy = 010; // SyntaxError: octal literals are not allowed
// let x = 1; delete x; // SyntaxError: delete of an unqualified identifier
// with (Math) { PI; } // SyntaxError: `with` is not allowed
// let eval = 1; // SyntaxError: eval cannot be a binding name
// let public = 1; // SyntaxError: reserved for future use
The reserved-word list is worth knowing because the errors look arbitrary: implements,
interface, let, package, private, protected, public, static and yield cannot be used
as names.
arguments stops tracking the parameters
function sloppy(a) {
a = 99;
return arguments[0]; // 99 — linked
}
function strict(a) {
'use strict';
a = 99;
return arguments[0]; // the value actually passed
}
console.log(sloppy(1)); // 99
console.log(strict(1)); // 1
Strict mode also removes arguments.callee and fn.caller, which existed for anonymous recursion
and stack inspection and are both long replaced — named function expressions for the first, and
error.stack for the second.
Where you already have it
// a .mjs file, or any file loaded with <script type="module">, or an ES module
// in a bundler — strict, with no directive:
export function f() {
// undeclared = 1; // ReferenceError
}
class Thing {
method() {
// class bodies are always strict too
return this;
}
}
Which means in a modern codebase the directive appears in exactly two places: a legacy plain script,
and a CommonJS file where you want it. Node treats .cjs and required .js as sloppy by default.
// sloppy.js (a plain script)
function sloppyFn() { return this; }
// module.mjs
import { sloppyFn } from './sloppy.js';
console.log(sloppyFn() === globalThis); // true — it kept its own dialect
Common mistakes
Putting the directive after something else
Mistake. A comment is fine, but a const, an import or any statement above 'use strict' is
not.
Why it fails. The directive is only recognised as the first statement of a file or function body;
anywhere else it is a discarded string, with no warning.
What to do instead. Put it on line one — or use a module and stop thinking about it.
Expecting it to catch type errors
Mistake. Treating strict mode as a type checker.
Why it fails. It changes a specific, closed list of behaviours. '5' - 1, undefined.length
guarded by ?., and a function called with the wrong number of arguments are all unaffected.
What to do instead. Strict mode for the silent failures, TypeScript or JSDoc types for the shape
of your data, and a linter for the rest.
Assuming a bundled file is sloppy
Mistake. Relying on this === globalThis inside a plain function in code that gets bundled.
Why it fails. The bundler emits modules, and modules are strict, so this becomes undefined
and the code throws in production but not in the quick test you ran in the console.
What to do instead. Use globalThis explicitly when you mean the global object.
Check yourself
2 questions from the bank on what you just read. Answering here does not affect your quiz history.
Which of these is true in strict mode but not in sloppy mode?
Where is strict mode already on, without you writing anything?
Further reading
Everything above is meant to be enough on its own. These are for when you want the full specification of a detail.
Reaching the end of a lecture marks it as read.