Strict Mode
What `'use strict'` changes, which silent failures it turns into thrown errors, and why ES modules and classes are already strict by default.
Strict Mode
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `'use strict'` is a directive, not a function call or a setting flipped elsewhere — placed as a plain string at the top of a script or a function body, it changes how the engine runs every line of code that follows it in that scope.
- It turns several mistakes that non-strict JavaScript allows silently into thrown errors instead — assigning to a variable you never declared, writing to a read-only property, and using the same parameter name twice all throw in strict mode.
- Inside a plain function call, `this` is `undefined` in strict mode instead of silently defaulting to the global object — a change that catches a whole category of bugs where a function expected to be called as a method winds up mutating global state by accident.
- You don't need to add the directive yourself inside an ES module or a class body — both are strict by default, which is a big part of why modern JavaScript, written with modules and classes, already behaves more safely than the language used to by default.
- It's a stricter rulebook for the parser and runtime to enforce, not a performance switch and not a security boundary — its entire value is catching your own mistakes earlier and louder than non-strict JavaScript would.
The problem strict mode was added to fix
JavaScript’s original design leaned hard toward never stopping your
program, even when something had clearly gone wrong. Misspell a variable
name in an assignment with no declaration anywhere, and instead of an
error telling you so, the language just quietly creates a new global
variable for you. Call a function that expects to run as a method, but
call it plainly instead, and this silently becomes the global object
rather than complaining. Both are the kind of mistake that doesn’t crash
your program — it just produces a wrong result somewhere downstream,
which is far harder to track back to its source than an error raised
right at the point of the mistake.
Strict mode exists to flip that tradeoff. It’s an opt-in (and, as covered below, sometimes automatic) mode that turns a specific list of “fail silently” behaviors into “fail loudly, right here” ones instead.
How you turn it on
'use strict' (or "use strict" — either quoting works) is written as a
plain string literal, with nothing else on the line, at the very top of
either a whole script or a single function body. The parser treats it
specially only because of that position — it’s not a real statement doing
anything at runtime, it’s a signal the engine reads before running
anything else in that scope. Put the same string in the middle of a
function and it does nothing at all; placement is everything.
What actually changes
The video covers the concrete, specific list of behaviors that flip once strict mode is active — which is the right way to learn this, since the value of strict mode is entirely in those specific changes, not in some general “safer” vibe. Two matter most in daily code:
Undeclared assignment throws. Write to a name with no var, let, or
const anywhere establishing it, and non-strict JavaScript creates a
global variable on the spot. Strict mode throws a ReferenceError
instead, which is almost always the behavior you actually want — a typo in
a variable name becomes an error at the line it happened, not a mystery
bug three screens of code later.
this stops defaulting to the global object. Call a plain function —
not attached to any object — and in non-strict mode, this inside it
quietly becomes the global object. In strict mode, it’s undefined
instead. That sounds like it’s making things worse (an error now, where
there wasn’t one before), but it’s the point: a function written to be
called as obj.method() that somehow gets called as a bare method()
should fail obviously, not go on to silently read or write properties on
the global object as if nothing were wrong.
Strict mode also disallows a handful of specific constructs outright —
duplicate parameter names in a function signature, assigning to variables
that JavaScript reserves for future use, and the with statement among
them — all cases where the non-strict behavior was either confusing or a
known source of bugs.
The part nobody has to opt into anymore
Here’s the detail that makes strict mode feel almost invisible in modern
code: you don’t need the directive at all inside an ES module or inside a
class body — both are strict by default, no string required. Given how
much current JavaScript is written as modules and classes, a large share
of the code anyone writes today is already running under strict mode’s
rules without a single 'use strict' line anywhere in the file. The
directive itself mostly matters now for older-style scripts or specific
functions outside that context.
Where this fits
Strict mode closes out this group because it’s really a response to
everything covered earlier in it — scope, hoisting, and the kind of
silent mistake those rules leave room for. It’s also the right bridge into
functions and this
next, since the this-binding change covered here is the first real hint
that this doesn’t mean one fixed thing in JavaScript — a topic that
group covers in full.
Check yourself
You assign to a variable you never declared with `var`, `let`, or `const` — say, `total = 5;` with no declaration anywhere. What happens in non-strict mode versus strict mode?
In non-strict mode, that silently creates a new property on the global object — it works, but it's almost never what you meant, and it pollutes global scope without any warning. In strict mode, the same line throws a `ReferenceError` immediately, because strict mode requires every variable to be explicitly declared before it's assigned.
Inside a plain function call (not a method call), what does `this` refer to in strict mode versus non-strict mode?
In non-strict mode, `this` silently falls back to the global object (`window` in a browser) when a function is called plainly, without being attached to an object. In strict mode, `this` is `undefined` in that same situation — a function expecting to be called as a method, then accidentally called plainly, fails loudly (trying to read a property of `undefined` throws) instead of quietly mutating global state.
Do you need to write `'use strict'` inside a class body or an ES module to get strict-mode behavior?
No — both are strict by default, with no directive required. This matters practically: if you're writing modern JavaScript with `import`/`export` and `class`, you're already getting strict mode's error-catching behavior for free, which is a big reason strict mode can feel like it "doesn't come up much" even though it's active in most code people write today.
Is `'use strict'` a function you call, like `strictMode()`? What is it actually?
No — it's a directive: a plain string literal, on its own line, that the parser recognizes specially because of where it sits (the very top of a script or function body). It has no return value and does nothing if placed somewhere else in the code, like in the middle of a function — it only takes effect from the top of the scope it's declared in.
Does enabling strict mode make your code run faster, or make it more secure against malicious input?
Neither, directly. Strict mode is purely about which mistakes the engine catches and how loudly — turning silent failures into thrown errors — not about optimizing execution speed or validating/sanitizing untrusted input. Any speed difference is incidental at best; security still has to be handled by your own input validation and logic, strict mode or not.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.