The "use strict"
directive in JavaScript enables strict mode, a stricter variant of JavaScript introduced in ECMAScript 5 (ES5). It enforces better coding practices, catches common errors, and prevents unsafe or deprecated behaviors. Here’s a breakdown of its purpose and effects:
What It Does
- Eliminates Silent Errors
- Throws errors for previously ignored actions like:
- Assigning to undeclared variables (prevents accidental global variables).
- Modifying read-only properties (e.g.,
NaN = 5
). - Deleting variables, functions, or function parameters (e.g.,
delete x;
).
- Disallows Problematic Syntax
- Blocks duplicate parameter names in functions (e.g.,
function foo(a, a) {}
). - Forbids octal literals (e.g.,
0123
becomes a syntax error; use0o123
instead). - Restricts reserved keywords (e.g.,
eval
,arguments
,implements
) from being used as variable names.
- Changes
this
Behavior
- In non-strict mode,
this
defaults to the global object (e.g.,window
in browsers) in functions not bound to an object. - In strict mode,
this
isundefined
in such cases, preventing accidental global pollution.
- Secures
eval
- Variables declared inside
eval
no longer leak into the outer scope. - Prevents
eval
from introducing new variables in the current scope.
- Miscellaneous Improvements
- Disallows assigning to
arguments
(e.g.,arguments = [...]
). - Removes
with
statements, which are error-prone and hinder optimization.
Reasoning Behind Strict Mode
- Safer Code
- Catches common coding mistakes (e.g., typos in variable names) and throws explicit errors.
- Prevents accidental global variables and unsafe operations.
- Optimization
- Allows JavaScript engines to optimize code more effectively by enforcing predictable behavior.
- Future-Proofing
- Bans syntax likely to conflict with future ECMAScript versions (e.g., reserved keywords).
- Best Practices
- Encourages declarative coding (e.g., using
let
/const
instead of undeclared variables).
How to Enable Strict Mode
Add "use strict";
to the top of a script or beginning of a function:
// Applies to the entire script
"use strict";
function doSomething() {
// Strict mode applies here
}
// Applies only to the function
function strictFunction() {
"use strict";
// Strict mode here
}
Example: Strict Mode vs. Non-Strict Mode
// Non-strict mode
function riskyCode() {
undeclaredVar = 10; // Creates a global variable (bad practice)
console.log(this); // Refers to the global object (e.g., window)
}
// Strict mode
function safeCode() {
"use strict";
undeclaredVar = 10; // Throws ReferenceError: undeclaredVar is not defined
console.log(this); // undefined (if not bound to an object)
}
When to Use It
- Always: Modern JavaScript projects should default to strict mode.
- Legacy Code: Gradually adopt strict mode by enabling it per function.
Browser Support
Strict mode is supported in all modern browsers and Node.js. Older browsers (pre-ES5, like IE9 and below) ignore it but are largely obsolete.
By enforcing stricter rules, "use strict"
helps you write cleaner, more reliable code and avoid pitfalls that lead to bugs.