JavaScript Strict Mode ("use strict")
JavaScript is a versatile and widely-used programming language, essential for web development. Strict mode is a pivotal feature in JavaScript, introduced to
What "use strict" Is
JavaScript was designed in 1995 to never crash on bad code — mistakes were silently ignored to keep web pages alive. That forgiving behavior hides bugs. Strict mode is an opt-in variant of the language, introduced in ECMAScript 5 (2009), that turns many of those silent mistakes into loud errors and removes a handful of confusing legacy features.
You switch it on with a literal string directive:
"use strict";This page covers where to put the directive, exactly what it changes, and why modern code (modules and classes) is already strict without you doing anything.
How to Enable Strict Mode
The directive must be the very first statement — a plain string, before any other code. If anything precedes it (even a stray expression), it is treated as an ordinary string and ignored, with no warning.
Whole script or module
Put "use strict"; at the top of the file to apply it to everything below:
"use strict";
// Every statement in this file now runs in strict mode.
function doStuff() {
// strict here too
}A single function
You can scope strict mode to one function by placing the directive at the top of its body. The rest of the file stays in sloppy ("non-strict") mode. This is useful when adding strict behavior to a large legacy file one function at a time.
function strictFn() {
"use strict";
return arguments; // strict behavior, just inside here
}
function sloppyFn() {
return arguments; // legacy behavior
}There is no way to turn strict mode off once a scope is strict. The directive is one-directional.
What Strict Mode Actually Changes
The differences are small in number but catch real bugs. Here are the ones you will meet most often.
Assigning to an undeclared variable throws
In sloppy mode, x = 10 with no var/let/const silently creates a global variable — a classic source of leaks and typos. Strict mode rejects it:
"use strict";
undeclaredVariable = 10; // ReferenceError: undeclaredVariable is not definedSee the global object and variable scope for why accidental globals are so harmful.
Bad assignments throw instead of failing silently
Writing to a read-only property, a getter-only property, or a non-extensible object fails quietly in sloppy mode. Strict mode throws a TypeError, so you find out immediately:
"use strict";
const obj = {};
Object.defineProperty(obj, "x", { value: 1, writable: false });
obj.x = 2; // TypeError: Cannot assign to read only property 'x'this is undefined in plain function calls
When you call a normal function directly (not as a method), sloppy mode sets this to the global object. Strict mode leaves it undefined, which surfaces mistakes where a method is detached from its object:
"use strict";
function whoAmI() {
return this;
}
console.log(whoAmI()); // undefined (in sloppy mode this would be the global object)Duplicate parameter names are banned
"use strict";
function add(a, a) { // SyntaxError: Duplicate parameter name not allowed in this context
return a + a;
}Other restrictions
deleteon a variable, function, or function argument is aSyntaxError.- Octal literals like
010are disallowed (use0o10). evalandargumentscannot be assigned to or used as variable names.- Reserved words like
implements,interface,private,publiccannot be used as identifiers.
Modules and Classes Are Always Strict
You often do not need to write "use strict"; at all, because two of the most common modern contexts are strict automatically:
- ES modules. Any file loaded as a module (
<script type="module">, animported file, or a.mjsfile) runs in strict mode. The directive is redundant there. - Class bodies. All code inside a
class { ... }declaration or expression is strict, regardless of the surrounding file.
class Counter {
// This method is strict even with no directive anywhere in the file.
bump() {
return this; // undefined if called detached, like any strict function
}
}Because of this, most code written today is effectively already in strict mode. The "use strict"; directive matters mainly for classic, non-module <script> files and older codebases.
When to Use It
- New non-module scripts: add
"use strict";at the top. It is free insurance against accidental globals and silent failures. - Modules and classes: nothing to do — you are already strict.
- Legacy files: adopt incrementally, function by function, and re-test. Switching a whole sloppy file to strict can expose code that secretly relied on accidental globals or sloppy
this. See the old "var" for related legacy behavior worth cleaning up at the same time.
Summary
Strict mode is a stricter, safer dialect of JavaScript turned on with the "use strict"; directive at the top of a script or function. It blocks implicit globals, throws on bad assignments, sets this to undefined in plain calls, and forbids duplicate parameters and a few legacy syntaxes. ES modules and class bodies enable it for you, so the main reason to write it by hand is for classic script files and older code.