JavaScript Strict ModeGet Certified
Strict mode is a way to opt into a restricted variant of JavaScript. It makes the language safer and less error-prone by turning silent mistakes into thrown errors and by disallowing certain unsafe features.
How to Enable Strict Mode
You enable strict mode by placing the directive
"use strict"; at the top of a script or a function.
"use strict";
// Entire script is now in strict mode
let message = "Hello from strict mode";
console.log(message);
"use strict"; must appear before any other statements.Function-level Strict Mode
function normalFunction() {
// This function is not strict
undeclared = 10; // Creates a global variable (bad)
}
function strictFunction() {
"use strict";
// This function is strict
// undeclared = 20; // Would throw a ReferenceError
}
console.log("Functions defined.");
"use strict"; inside the function makes only that function strict.Preventing Accidental Global Variables
In normal mode, assigning to an undeclared variable silently creates
a global variable. Strict mode throws a ReferenceError
instead.
"use strict";
try {
total = 100; // ReferenceError
} catch (err) {
console.log("Error:", err.message);
}
total without declaring it throws a ReferenceError.Writing to Read-only Properties
In strict mode, attempting to write to a read-only property throws
a TypeError instead of failing silently.
"use strict";
const obj = {};
Object.defineProperty(obj, "name", {
value: "Michael",
writable: false
});
try {
obj.name = "Alex"; // TypeError
} catch (err) {
console.log("Error:", err.message);
}
writable: false.TypeError when the assignment is attempted.Duplicate Function Parameter Names
Strict mode forbids having two parameters with the same name.
"use strict";
// This would cause a SyntaxError in strict mode:
// function bad(a, a) {
// return a;
// }
function good(a, b) {
return a + b;
}
console.log(good(3, 4));
The value of this
In a plain function call, strict mode sets this to
undefined instead of the global object.
"use strict";
function showThis() {
console.log(this); // undefined
}
showThis();
this is undefined.The delete Operator
Strict mode makes delete throw a SyntaxError
when used on a variable, function, or function parameter.
"use strict";
let x = 10;
try {
// delete x; // SyntaxError in strict mode
console.log("delete on a variable is not allowed in strict mode.");
} catch (err) {
console.log(err.message);
}
Reserved Words as Identifiers
Strict mode forbids using certain future reserved words as variable or function names.
"use strict";
// These would cause a SyntaxError:
// let interface = "test";
// let package = 1;
// let private = true;
console.log("Reserved words cannot be used as identifiers in strict mode.");
Strict Mode vs. Normal Mode
| Behavior | Normal Mode | Strict Mode |
|---|---|---|
| Assigning to an undeclared variable | Creates a global variable | Throws a ReferenceError |
| Writing to a read-only property | Fails silently | Throws a TypeError |
| Duplicate function parameter names | Allowed | SyntaxError |
this in a plain function call |
The global object | undefined |
delete on a variable |
Allowed (often silently ignored) | SyntaxError |
| Using future reserved words as identifiers | Allowed | SyntaxError |
Practical Example
Strict mode helps catch a common typo-based bug early.
"use strict";
function calculateTotal(price, quantity) {
// Typo: "totl" instead of "total"
try {
totl = price * quantity;
} catch (err) {
console.log("Caught a typo bug:", err.message);
}
}
calculateTotal(10, 5);
ReferenceError, which is caught and reported.Common Mistakes With Strict Mode
- Placing
"use strict";after other statements, where it has no effect. - Assuming strict mode is applied automatically everywhere, when scripts and regular functions require it explicitly.
- Forgetting that classes and ES6 modules are always strict, and being surprised by strict-mode behavior inside them.
- Relying on
thisbeing the global object inside a plain function call, which breaks in strict mode. - Mixing strict and non-strict code without understanding which parts of the file are affected.
Best Practices for Strict Mode
- Enable strict mode at the top of every script or module.
- Rely on modules or classes, which are strict by default, when possible.
- Use strict mode to catch bugs like typos and accidental globals early during development.
- Avoid using words that are reserved for future JavaScript features as variable names.
- Test code thoroughly after enabling strict mode, since some previously “working” (but buggy) code may now throw errors.
Quick Summary
- Strict mode is enabled with the
"use strict";directive, either globally or inside a single function. - Classes and ES6 modules are strict by default, with no directive needed.
- Strict mode prevents accidental global variables and turns silent failures into thrown errors.
- Duplicate function parameters and certain reserved words are disallowed in strict mode.
thisisundefinedin a plain function call under strict mode, instead of the global object.- Strict mode is widely considered a best practice for writing safer, more predictable JavaScript.
Try It Yourself
Try removing the "use strict"; line below and running
the code again. Notice how the behavior changes when the undeclared
variable assignment no longer throws an error.
"use strict";
try {
total = 100;
console.log(total);
} catch (err) {
console.log("Error:", err.message);
}
Tip: Think of strict mode as JavaScript refusing to guess what you meant. Instead of quietly patching over a mistake, it stops and tells you exactly where things went wrong — which saves time in the long run, even if it feels stricter upfront.
Understanding strict mode is useful before moving on to functions, scope, classes, and more advanced JavaScript concepts.
🧪 Test Your JavaScript Code
Edit the code on the left and click “Run Code” to see the console output on the right.
Click “Run Code” to see the output here.
Code Explanation
The complete example stays visible. Use Previous and Next to move through its logical blocks.
Select Next to begin exploring the code.