Strict mode is a special mode that makes JavaScript behave in a safer, cleaner, and more predictable way. You can enable it manually or it’s automatically enabled in certain contexts like ES modules.
Understanding strict mode helps you catch common mistakes early and write more maintainable code.
What Is Strict Mode?
Strict mode is a way to opt into a restricted variant of JavaScript. It makes JavaScript behave more strictly by catching silent errors, preventing certain actions, and improving performance in some cases.
You can enable it manually with:
'use strict';
Or automatically (it’s on by default in modules like type=“module”).
Why It Exists
JavaScript’s early design was very lenient. It allowed silent mistakes that could lead to bugs. For example, assigning to an undeclared variable would automatically create a global variable instead of throwing an error.
Strict mode helps catch those mistakes early by enforcing better rules.
What Strict Mode Changes
| Behavior | Without Strict Mode | With Strict Mode |
|---|---|---|
| Undeclared variables | Allowed (creates global variable) | Throws an error |
| Duplicate parameter names | Allowed | Error |
| this in functions | Refers to window | undefined unless explicitly bound |
| Silent errors | Ignored | Throws exceptions |
| Deleting variables/functions | Allowed | Error |
| Reserved keywords | May be used accidentally | Protected for future JS features |
Example: Undeclared Variables
// Without strict mode
x = 10; // Creates a global variable (bug!)
console.log(x); // 10
// With strict mode
'use strict';
y = 20; // ReferenceError: y is not defined
This prevents accidental global variable creation, which is a common source of bugs.
Example: this Binding
function show() {
console.log(this);
}
// Without strict mode
show(); // window (in browser)
// With strict mode
'use strict';
show(); // undefined
This makes the behavior of this more predictable and prevents accidentally modifying the global object.
Example: Duplicate Parameters
// Without strict mode
function add(a, a, b) {
return a + a + b;
}
// With strict mode
'use strict';
function add(a, a, b) {
// SyntaxError: Duplicate parameter name not allowed in this context
return a + a + b;
}
Example: Deleting Variables
'use strict';
var x = 10;
delete x; // SyntaxError: Delete of an unqualified identifier in strict mode
Example: Reserved Keywords
Strict mode protects certain keywords that are reserved for future JavaScript features:
'use strict';
var let = 10; // SyntaxError
var private = 20; // SyntaxError
var implements = 30; // SyntaxError
Enabling Strict Mode
Global Scope
To enable strict mode for an entire script file:
'use strict';
function test() {
// strict mode applies here
}
var x = 10;
// strict mode applies here too
Function Scope
To enable strict mode for a specific function:
function test() {
'use strict';
// strict mode only applies within this function
y = 20; // Error
}
x = 10; // No error (outside strict mode)
Module Scope (Automatic)
All ES modules automatically run in strict mode. You don’t need to write “use strict” yourself.
// module.js
// Automatically in strict mode
export function test() {
y = 20; // Error
}
Why It Matters for Modules
All ES modules (type=“module”) automatically run in strict mode. That means:
- You can’t create accidental globals
- Errors are caught early
- Your code behaves consistently across browsers
<script type="module">
// Automatically in strict mode
x = 10; // Error
</script>
Performance Benefits
Strict mode can also improve performance in some JavaScript engines because:
- It eliminates some ambiguities that make optimization difficult
- It prevents certain actions that are hard to optimize
- It allows engines to generate faster code
Common Errors Caught by Strict Mode
- Assignment to undeclared variable
'use strict';
x = 10; // ReferenceError
- Assignment to non-writable property
'use strict';
const obj = {};
Object.defineProperty(obj, 'x', { value: 10, writable: false });
obj.x = 20; // TypeError
- Deleting undeletable property
'use strict';
delete Object.prototype; // TypeError
- Octal literals
'use strict';
var x = 010; // SyntaxError
- with statement
'use strict';
with (obj) { // SyntaxError
// code
}
When to Use Strict Mode
You should use strict mode:
- Always in new code
- In all modules (it’s automatic)
- In functions that need to be extra safe
- When refactoring legacy code (gradually)
You might not use strict mode:
- In very old codebases that rely on non-strict behavior
- When dealing with third-party libraries that don’t support it
Summary
Strict mode equals “No more silent bugs.” It enforces clean, intentional JavaScript, and that’s why all modern code (including modules) uses it by default.
| Feature | Without Strict Mode | With Strict Mode |
|---|---|---|
| Undeclared variables | Creates global | Error |
| Duplicate params | Allowed | Error |
| Delete variables | Allowed | Error |
| this in functions | Global object | undefined |
| Octal literals | Allowed | Error |
| with statement | Allowed | Error |
| Reserved keywords | May use | Protected |
Strict mode makes JavaScript more predictable, catches common mistakes early, and is the foundation for modern JavaScript development. All new JavaScript code should be written with strict mode in mind.