What Is Strict Mode?
Strict mode is a feature introduced in ECMAScript 5 (ES5). It makes JavaScript behave in a stricter way by restricting certain actions and throwing errors for unsafe practices.
Strict mode can be enabled for the entire script or specific functions. When active, it helps developers avoid pitfalls like accidental global variables or duplicate parameter names.
How to Enable Strict Mode
You can enable strict mode using the “use strict”; directive at the beginning of your script or function.
Enabling Strict Mode for the Entire Script
"use strict";
x = 10; // Error: x is not defined
console.log(x);
Enabling Strict Mode for a Specific Function
function strictFunction() {
"use strict";
y = 20; // Error: y is not defined
}
strictFunction();
Benefits of Strict Mode
- Prevents Accidental Globals:
Variables must be explicitly declared, reducing errors caused by undeclared variables. - Eliminates Silent Errors:
Converts silent errors into exceptions, making debugging easier. - Disallows Unsafe Actions:
Forbids certain syntax and features that can cause unpredictable behavior. - Encourages Modern JavaScript Practices:
Restricts outdated and deprecated syntax. - Improves Performance:
Strict mode allows JavaScript engines to optimize code execution by identifying common pitfalls.
Real-World Examples
Example 1: Preventing Accidental Globals
Without strict mode:
x = 100; // No error, x becomes a global variable
console.log(x); // Output: 100
With strict mode:
"use strict";
x = 100; // Error: x is not defined
console.log(x);
Example 2: Safer this
Behavior
Without strict mode:
function nonStrictFunction() {
console.log(this); // Output: [object Window]
}
nonStrictFunction();
With strict mode:
"use strict";
function strictFunction() {
console.log(this); // Output: undefined
}
strictFunction();
When Not to Use Strict Mode
While strict mode is beneficial, there are situations where it might not be ideal:
- Legacy Code: Adding strict mode to older codebases may introduce unexpected errors.
- Libraries: Avoid enabling strict mode globally in shared libraries to prevent conflicts.
Key Rules of Strict Mode
Variables Must Be Declared:
Undeclared variables throw an error in strict mode.
"use strict";
myVar = 50; // Error: myVar is not defined
No Duplicate Parameter Names:
Functions cannot have duplicate parameter names.
"use strict";
function add(a, a) { // Error: Duplicate parameter name not allowed
return a + a;
}
Immutable this
in Functions:
In non-strict mode, this refers to the global object (window in browsers). In strict mode, it remains undefined if not explicitly set.
"use strict";
function showThis() {
console.log(this); // Output: undefined
}
showThis();
Reserved Keywords Are Restricted:
You cannot use future-reserved keywords like implements, interface, or public.
"use strict";
let public = 10; // Error: Unexpected strict mode reserved word
Disallows Deleting Variables or Functions:
Deleting variables or functions throws an error.
"use strict";
let num = 5;
delete num; // Error: Cannot delete 'num'
Forbids Octal Literals:
Octal literals are not allowed in strict mode.
"use strict";
let num = 010; // Error: Octal literals are not allowed in strict mode
Disallows with Statements:
The with statement is forbidden because it creates ambiguity in variable scope.
"use strict";
with (Math) { // Error: Strict mode code may not include a 'with' statement
console.log(sqrt(16));
}