Strict mode in JavaScript
Table of contents
- How to Enable Strict Mode
- Benefits and Features of Strict Mode
- 1. Prevents the Use of Undeclared Variables
- 2. Eliminates this Binding to Global Object
- 3. Disallows Duplicates in Object Literals
- 4. Disallows Duplicates in Function Parameters
- 5. Throws Errors for Invalid Deletions
- 6. Prohibits with Statement
- 7. Prevents Octal Syntax
- 8. eval Restrictions
- 9. Secures JavaScript from Certain Mistakes
- Compatibility and Usage
- Example: Combining Features
"Strict mode" in JavaScript is a way to opt into a restricted variant of the language, which aims to improve code quality and catch common mistakes and errors. It was introduced in ECMAScript 5 (ES5). Enabling strict mode makes several changes to normal JavaScript semantics, such as eliminating some silent errors by changing them to throw errors, fixing mistakes that make it difficult for JavaScript engines to perform optimizations, and prohibiting some syntax likely to be defined in future versions of ECMAScript.
How to Enable Strict Mode
Strict mode can be enabled in two ways:
Globally for an entire script: By placing the
"use strict";
directive at the top of a script file."use strict"; // All code in this file will be in strict mode function example() { return "Example function"; }
Locally for a function: By placing the
"use strict";
directive at the beginning of a function body.function example() { "use strict"; // This function will be in strict mode return "Example function"; } // Code outside the function will not be in strict mode
Benefits and Features of Strict Mode
1. Prevents the Use of Undeclared Variables
In non-strict mode, assigning a value to an undeclared variable implicitly creates a global variable. In strict mode, this throws an error.
"use strict";
x = 3.14; // Throws an error because x is not declared
2. Eliminates this
Binding to Global Object
In non-strict mode, this
inside a function refers to the global object (window in browsers). In strict mode, this
remains undefined
unless explicitly set.
"use strict";
function example() {
console.log(this); // undefined
}
example();
3. Disallows Duplicates in Object Literals
Strict mode disallows duplicate property names in object literals.
"use strict";
let obj = {
name: "John",
name: "Doe" // SyntaxError: Duplicate data property in object literal not allowed in strict mode
};
4. Disallows Duplicates in Function Parameters
Strict mode disallows duplicate parameter names in function declarations.
"use strict";
function example(a, a, b) {
// SyntaxError: Duplicate parameter name not allowed in this context
}
5. Throws Errors for Invalid Deletions
In strict mode, attempting to delete undeletable properties throws an error.
"use strict";
delete Object.prototype; // TypeError: Cannot delete property 'prototype' of function Object() { [native code] }
6. Prohibits with
Statement
The with
statement is not allowed in strict mode, as it can make code harder to predict and optimize.
"use strict";
with (Math) {
// SyntaxError: Strict mode code may not include a with statement
}
7. Prevents Octal Syntax
Octal numeric literals are not allowed in strict mode.
"use strict";
let num = 023; // SyntaxError: Octal literals are not allowed in strict mode
8. eval
Restrictions
In strict mode, eval
does not introduce new variables into the surrounding scope.
"use strict";
eval("var x = 2;");
console.log(x); // ReferenceError: x is not defined
9. Secures JavaScript from Certain Mistakes
Strict mode also throws errors for common mistakes, such as assigning to read-only properties or writing to NaN
.
"use strict";
NaN = 123; // TypeError: Assignment to constant variable.
Compatibility and Usage
Strict mode is supported in all modern browsers and environments that comply with ECMAScript 5 and later. It’s a best practice to use strict mode to catch common coding issues early and avoid potential pitfalls.
Combining with ES6 Modules
In ES6 modules, strict mode is enabled by default, so there is no need to include the "use strict";
directive explicitly.
Example: Combining Features
"use strict";
// Prevents undeclared variables
let a = 2;
a = 3; // Allowed because 'a' is declared
// Prevents duplicate property names
let obj = {
prop1: "value1",
prop2: "value2"
};
// Prevents duplicate parameter names
function example(param1, param2) {
// 'param1' and 'param2' must be unique
return param1 + param2;
}
// Disallows 'with' statement
// 'with' (Math) { console.log(sqrt(4)); } // SyntaxError
// Prevents octal literals
let octalNumber = 0o10; // Use 0o prefix for octal literals in strict mode
console.log(octalNumber); // 8
// 'eval' does not introduce variables into the surrounding scope
eval("var x = 10;");
console.log(typeof x); // undefined
By understanding and utilizing strict mode, you can write more robust and secure JavaScript code, reduce the likelihood of common bugs, and improve performance through better optimization by JavaScript engines.