Strict Mode Is a Stricter Variant of JavaScript
JavaScript has a long history, and it carries a lot of questionable decisions with it — assignments that silently create globals, a this that changes meaning depending on how you call a function, duplicate parameter names that just... worked. Breaking any of those would break the web, so TC39 (the committee that maintains the language) took a different route: an opt-in mode where the sharp edges get filed down.
That opt-in is called strict mode. You turn it on with a single line at the top of a file or function:
Run that and you'll get a ReferenceError: x is not defined. Without the directive, the same code happily creates a global variable named x and prints 10. Same language, two different sets of rules.
The 'use strict' Directive
The directive is just a string literal — 'use strict'; or "use strict";. It has to be the first statement of the script or function. Anything before it, even a stray expression, and the engine treats it as a regular string and ignores it.
You can scope strict mode to a single function by putting the directive inside it. In practice, nobody does this anymore — you either write modules (strict by default) or flip the whole file.
What Strict Mode Actually Changes
Strict mode isn't one rule; it's a bundle. The highlights:
- Assigning to an undeclared variable throws instead of creating a global.
thisinside a regular function call isundefined, not the global object.- Duplicate parameter names (
function f(a, a) {}) are a syntax error. - Assigning to read-only properties throws instead of failing silently.
- Deleting a plain variable or function throws.
- A few future-reserved words (
implements,interface,package,private,protected,public,static,yield) can't be used as variable names.
A small tour:
Every one of those is "the engine noticed a likely mistake earlier." That's the whole value proposition.
The Silent Global Bug
This is the single biggest reason strict mode exists. Without it, a typo turns into a global variable:
// Non-strict mode (don't do this)
function setup() {
usernmae = 'Ada'; // typo — creates window.usernmae
}
setup();
console.log(username); // undefined — the real variable was never set
The program runs. No error. The bug only shows up later, when something that should have read username finds undefined. In strict mode, that same typo throws the moment it runs, and you fix it in seconds instead of hours.
Modern JavaScript Is Strict by Default
Here's the part that trips people up: most JavaScript you write today is already in strict mode, and you didn't type 'use strict' anywhere.
Two big sources of automatic strictness:
- ES modules. Any
.mjsfile, any<script type="module">, and anything imported withimport/exportruns in strict mode. No directive needed. - Class bodies. Every statement inside a
class { ... }is strict, even if the surrounding file isn't.
So when do you actually need to write 'use strict'; yourself? Only in classic scripts — old-style JS loaded with a plain <script> tag, or older Node.js files that use require without module bundling. For anything new, modules do the work for you.
Strict Mode Changes this in Plain Functions
One behavior worth calling out because it bites newcomers. In non-strict mode, calling a regular function without an object in front of it binds this to the global object (window in browsers, global in Node). In strict mode, it binds to undefined:
That's usually what you want. If this is undefined, a bug where you forgot to bind a method surfaces immediately. If it silently points at the global object, the bug hides until something further down the line breaks.
What Strict Mode Does Not Do
Strict mode is a compile-time and runtime tightening — it doesn't give you types, doesn't catch null dereferences, doesn't warn about unused variables. Those are jobs for TypeScript, ESLint, and your editor. Think of strict mode as fixing a specific set of language-level footguns, not as a full safety net.
And it's not the same thing as "modern JavaScript." You can write terrible code in strict mode and excellent code without the directive. It just makes a few specific mistakes harder to make.
What You Take Away
'use strict';at the top of a file or function opts into a stricter variant of JavaScript.- Typos stop creating globals,
thisstops silently becoming the global object, and a handful of other footguns become errors. - ES modules and
classbodies are strict automatically — you rarely need to write the directive by hand. - It's about catching bugs earlier, not about enabling new features.
Next: Comments
You've seen directives, semicolons, and the rules the engine cares about. Next up: the notes you leave for future readers — how JavaScript comments work, and when they're worth writing.
Frequently Asked Questions
What is strict mode in JavaScript?
Strict mode is an opt-in variant of JavaScript that turns a handful of silent mistakes into real errors. You enable it by putting the string 'use strict'; at the top of a file or function. It tightens the language — assigning to an undeclared variable throws, duplicate parameter names are illegal, and this inside a plain function call is undefined instead of the global object.
How do I enable strict mode in JavaScript?
Put 'use strict'; as the very first statement of a script or function. Nothing else — not even a comment-free blank line — can come before it, or it's treated as a normal string. In modern code you rarely write it yourself: ES modules (.mjs files or <script type="module">) and the body of any class are strict automatically.
Why use strict mode in JavaScript?
It catches bugs earlier. A typo like usernmae = 'Ada' silently creates a global in non-strict mode; in strict mode it throws a ReferenceError. Strict mode also reserves future keywords, disallows duplicate property names in older engines, and makes this behave predictably — all of which make code easier to reason about.
Do I need 'use strict' in modern JavaScript?
Usually no. ES modules and class bodies are strict by default, and most modern projects are one or the other. You only need to write 'use strict'; explicitly in old-style classic scripts — the kind loaded with a plain <script> tag without type="module".