1.5 Strict Mode
What is Strict Mode?
JavaScript's Strict Mode is a feature introduced in ES5 that applies stricter rules instead of JavaScript's loose (sloppy) default behavior.
"use strict"; // This one line enables strict mode
// Stricter rules now apply
Enabling Strict Mode
Global Level (Entire File)
"use strict";
// Strict mode applies to entire file
function example() {
// This function is also in strict mode
}
Function Level
function strictFunction() {
"use strict"; // Applies only to this function
// Strict mode
}
function normalFunction() {
// Non-strict mode
}
ES Modules (Automatically Applied)
// Using import/export automatically enables strict mode
import { something } from './module.js';
// Strict mode even without "use strict"!
Classes (Automatically Applied)
class MyClass {
// Class body is automatically in strict mode
method() {
// Strict mode
}
}
Non-strict vs Strict Mode Differences
1. Undeclared Variables Forbidden
// Non-strict mode: undeclared variable creates global variable (source of bugs!)
function nonStrict() {
x = 10; // No declaration → creates global variable x
console.log(x); // 10
}
nonStrict();
console.log(x); // 10 (global pollution!)
// Strict mode: ReferenceError thrown
function strict() {
"use strict";
y = 20; // ReferenceError: y is not defined
}
strict();
2. this Binding Difference
// Non-strict mode: this in regular function is global object (window/global)
function showThis() {
console.log(this); // window (browser) or global (Node.js)
}
showThis();
// Strict mode: this in regular function is undefined
function showThisStrict() {
"use strict";
console.log(this); // undefined
}
showThisStrict();
3. Duplicate Parameters Forbidden
// Non-strict: duplicate parameters allowed (last value used)
function duplicate(a, a, b) {
console.log(a, b); // second a value used
}
duplicate(1, 2, 3); // 2 3
// Strict mode: SyntaxError
function strictDuplicate(a, a, b) { // SyntaxError: Duplicate parameter name
"use strict";
}
4. with Statement Forbidden
const obj = { x: 10, y: 20 };
// Non-strict: with statement allowed (performance and readability issues)
with (obj) {
console.log(x + y); // 30
}
// Strict mode: SyntaxError
"use strict";
with (obj) { // SyntaxError: Strict mode code may not include a with statement
console.log(x + y);
}
5. delete Restrictions
// Non-strict: delete failure returns false silently
var x = 10;
console.log(delete x); // false (silent failure)
// Strict mode: SyntaxError
"use strict";
var y = 20;
delete y; // SyntaxError: Delete of an unqualified identifier in strict mode
6. eval Scope Isolation
// Non-strict: eval can add variables to current scope
eval("var leaked = 'I am leaked!'");
console.log(leaked); // 'I am leaked!'
// Strict mode: eval has its own scope
"use strict";
eval("var notLeaked = 'isolated'");
console.log(notLeaked); // ReferenceError: notLeaked is not defined
7. Writing to Read-only Properties Forbidden
// Non-strict: silently fails
const obj = {};
Object.defineProperty(obj, 'readOnly', { value: 42, writable: false });
obj.readOnly = 100; // Silent failure
console.log(obj.readOnly); // 42 (unchanged)
// Strict mode: TypeError
"use strict";
obj.readOnly = 100; // TypeError: Cannot assign to read only property
Why Use Strict Mode
Bug Prevention
// Common mistake: typo creates new global variable
"use strict";
let userName = "Alice";
userNmae = "Bob"; // ReferenceError! (typo: userNmae)
// Without strict mode, silently creates global variable → hard to debug
Future Compatibility
Strict mode prevents use of reserved words that may become keywords in future ECMAScript versions:
"use strict";
// These identifiers are reserved in strict mode:
// implements, interface, let, package, private, protected, public, static, yield
let implements = 1; // SyntaxError (reserved word)
Strict Mode Summary
| Restriction | Non-strict | Strict |
|---|---|---|
| Undeclared variables | Creates global | ReferenceError |
this in functions | Global object | undefined |
| Duplicate parameters | Allowed | SyntaxError |
with statement | Allowed | SyntaxError |
Variable delete | false (silent) | SyntaxError |
| Write to read-only | Silent failure | TypeError |
eval scope | Pollutes current scope | Isolated scope |
Modern JavaScript Always Uses Strict Mode
// 1. ES Modules (import/export) → automatically strict
import { helper } from './utils.js';
// 2. Classes → automatically strict
class MyService {
process() { /* strict mode */ }
}
// 3. TypeScript → always compiled with strict mode
// 4. Bundlers (Vite, webpack) → typically enable strict mode
For legacy .js files (not modules), explicitly adding "use strict" at the top is recommended.