JavaScript strict mode is a way to enforce stricter rules for writing JavaScript code. It was introduced in ECMAScript version 5 to help developers write more reliable and bug-free code. When strict mode is enabled, certain JavaScript behavior that is considered to be error-prone or incoherent is changed to throw exceptions or produce errors, making it easier to catch and fix potential issues.
To enable strict mode, you can add a special directive at the beginning of a script or a function by using the following code:
"use strict";
Once strict mode is enabled, it applies to the entire script or function it is declared in, and it cannot be disabled within that scope.
Here are a few examples of JavaScript code snippets that demonstrate the use of strict mode:
1. Declare variables before use:
"use strict";
x = 10; // ReferenceError: x is not defined
2. Prevent assigning values to undefined variables:
"use strict";
var x;
y = 20; // ReferenceError: y is not defined
3. Prohibit deleting variables, functions, or function arguments:
"use strict";
var x = 10;
delete x; // SyntaxError: Delete of an unqualified identifier in strict mode.
function foo() {
"use strict";
delete x; // SyntaxError: Delete of an unqualified identifier in strict mode.
}
4. Restrict octal literals and escape characters:
"use strict";
var x = 0123; // SyntaxError: Octal literals are not allowed in strict mode.
var str = "\123"; // SyntaxError: Octal escape sequences are not allowed in strict mode.
5. Change the value of "this" in non-method functions:
"use strict";
function foo() {
console.log(this); // undefined
}
foo();
These examples showcase some of the strict mode rules and how they affect the behavior of JavaScript code. By enabling strict mode, you can catch errors early and produce more reliable and maintainable code.
Here are some of the effects and benefits of using strict mode:
1. Variables must be declared before use: In non-strict mode, you can use variables without declaring them first, which implicitly creates a global variable. In strict mode, however, this behavior is not allowed, and using a variable before it is declared will result in a ReferenceError.
2. Assigning values to undefined variables is prohibited: Non-strict mode allows you to assign values to variables that have not been declared, implicitly creating a global variable. This can lead to hard-to-diagnose bugs. But in strict mode, assigning a value to an undeclared variable is not allowed and will throw a ReferenceError.
3. Deleting variables, functions, or function arguments is not allowed: In strict mode, you cannot delete variables, function names, or function arguments. Attempting to delete such entities will throw a SyntaxError.
4. Octal literals and escape characters are restricted: In non-strict mode, you can use octal literals (e.g., 0123) and escape special characters in strings (e.g., "\123"). In strict mode, these behaviors are disallowed as they can lead to confusion and errors.
5. The value of "this" in functions is different: In non-strict mode, the value of the "this" keyword inside a function that is not a method (i.e., not bound to an object) refers to the global object (e.g., window in a browser environment or global in Node.js). In strict mode, "this" is undefined in such cases.
These are just a few examples of how strict mode alters the behavior of JavaScript code. Overall, strict mode helps prevent common mistakes and enforces best practices, leading to more robust and maintainable code. It is recommended to use strict mode in all JavaScript projects to catch errors early and improve code quality.