When ES5 was released, JavaScript introduced a new feature called "Strict Mode", designed to make the language safer, cleaner, and more predictable. While it might seem like a small thing, "use strict" can make a huge difference in the quality and stability of your code.
Let’s dive into what strict mode is, why it exists, and why you should always use it in your programs.
đź§© What Is Strict Mode?
Strict Mode tightens JavaScript’s rules and removes some of the language’s silent errors. It helps you write better, more reliable code by enforcing stricter parsing and error handling.
In short, strict mode:
Prevents the use of undeclared variables
Throws more meaningful errors
Disables dangerous or confusing features
Helps the JavaScript engine optimize performance
As Kyle Simpson mentions in You Don’t Know JS: Types & Grammar:
“Strict mode is a big win for code, and you should use it for all your programs.”
đź§ How to Enable Strict Mode
You can enable strict mode globally or per function, depending on where you place "use strict".
🔹 Global Strict Mode
When you place it at the top of your file, the entire file runs in strict mode:
"use strict";
function foo() {
// strict mode is active here
function bar() {
// strict mode is active here too
}
}
🔹 Function-Level Strict Mode
You can also enable strict mode inside an individual function:
function foo() {
"use strict";
// only this function runs in strict mode
function bar() {
// not in strict mode
}
}
⚙️ What Happens When You Use Strict Mode
Strict mode makes JavaScript behave in a more predictable and professional way. Here are some of the biggest improvements:
đźš« 1. No Implicit Global Variables
Without strict mode, assigning a value to an undeclared variable automatically creates a global variable — a common source of bugs.
function foo() {
a = 10; // creates a global variable (bad!)
}
foo();
console.log(a); // 10
With strict mode:
function foo() {
"use strict";
a = 10; // ❌ ReferenceError: a is not defined
}
foo();
This forces you to declare variables properly with let, const, or var.
đź§± 2. Prevents Silent Failures
In non-strict mode, some operations fail silently. For example, assigning a value to a read-only property doesn’t throw an error — it just fails quietly.
Strict mode changes that:
"use strict";
const obj = {};
Object.defineProperty(obj, "x", { value: 42, writable: false });
obj.x = 9; // ❌ TypeError
đź”’ 3. Disallows Duplicate Parameter Names
Strict mode doesn’t allow you to use the same parameter name more than once — which avoids confusion and improves readability.
"use strict";
function sum(a, a, c) { // ❌ SyntaxError
return a + a + c;
}
⚠️ 4. Prevents this from Being Global
In non-strict mode, if you call a function without a context, this defaults to the global object (window in browsers).
function showThis() {
console.log(this); // window
}
showThis();
But in strict mode, this stays undefined — which is safer and prevents accidental global variable leaks.
"use strict";
function showThis() {
console.log(this); // undefined
}
showThis();
💡 Why You Shouldn’t Avoid Strict Mode
If turning on strict mode makes your code break, that’s a good thing — it means strict mode is revealing potential issues in your program.
“If strict mode causes issues in your program, it’s almost certainly a sign that you have things you should fix.”
— You Don’t Know JS: Types & Grammar
Fixing these issues will make your code cleaner, more maintainable, and more future-proof.
🛠️ Strict Mode and Modern JavaScript
If you’re using ES6 modules (import / export), you don’t need to manually enable strict mode — it’s automatically applied.
// This module is always in strict mode
export function greet() {
message = "Hello!"; // ❌ ReferenceError
}
So modern JavaScript essentially encourages best practices by default.
đź§ Final Thoughts
Strict mode isn’t just a “developer annoyance” — it’s a tool for writing better JavaScript.
âś… It catches common mistakes early
âś… It improves performance
âś… It aligns with the modern direction of JavaScript
So don’t delay — make "use strict" (or ES modules) your default. Your future self will thank you.
Top comments (0)