“Use strict” is a directive in JavaScript that allows developers to opt-in to a stricter mode of code execution. It was introduced in ECMAScript 5 to make JavaScript code more robust and to avoid common coding mistakes. In this article, we will explore what “use strict” does in JavaScript and the reasoning behind it.
Preventing the use of undeclared variables
In non-strict mode JavaScript, it is possible to use a variable without declaring it first. If you use a variable that has not been declared, JavaScript will create it as a global variable. This can lead to hard-to-debug issues, especially in large codebases.
In strict mode, using an undeclared variable will result in an error. This can help catch bugs early and make the code more robust. Additionally, in strict mode, assigning a value to a variable that has not been declared will also result in an error.
Here is an example of how strict mode prevents the use of undeclared variables:
function exampleFunction() {
"use strict";
x = 10; // This will cause an error in strict mode
}
In this example, the variable x
is not declared anywhere. In non-strict mode, this code would create a global variable x
and assign it the value of 10. In strict mode, however, an error will be thrown because x
has not been declared.
Eliminating the use of with statements
The with
statement in JavaScript allows you to execute a block of code as if it were part of an object’s scope. For example:
const obj = { prop: "value" };
with (obj) {
console.log(prop); // "value"
}
While this can be a convenient shorthand in some cases, it can also lead to unexpected behavior and performance issues. For example, if the obj
object has a property with the same name as a variable in the outer scope, the with
statement can cause the wrong value to be accessed or modified.
In strict mode, the with
statement is not allowed, and attempting to use it will result in a syntax error. This eliminates the potential issues with with
and helps make the code more predictable and easier to debug.
Here is an example of how strict mode disallows the use of with
:
function exampleFunction() {
"use strict";
const obj = { prop: "value" };
with (obj) { // This will cause a syntax error in strict mode
console.log(prop);
}
}
In this example, trying to use with
inside the exampleFunction()
will result in a syntax error in strict mode. This helps prevent unexpected behavior and makes the code more reliable.
Making eval() safer:
The eval()
function in JavaScript allows you to execute a string of code as if it were part of the current scope. However, this can be dangerous because it can lead to code injection attacks or unexpected behavior.
In strict mode, the eval()
function has a few key differences:
- It creates a new scope for the evaluated code, so any variables or functions declared inside the evaluated code do not leak into the outer scope.
- The
eval()
function cannot create variables or functions in the outer scope. - The
this
keyword inside the evaluated code is not bound to the global object.
These changes make eval()
safer to use in certain cases, but it should still be used with caution.
Preventing duplicate object properties:
In JavaScript, it is possible to define an object with duplicate property names. For example:
const obj = {
prop1: "value1",
prop1: "value2"
};
In non-strict mode, this code would run without error, and the obj
object would have a single property named prop1
with the value of “value2”. This can be confusing and lead to bugs in your code.
In strict mode, however, this code would throw a syntax error, because duplicate properties are not allowed.
Disallowing the use of reserved words:
JavaScript has a number of reserved words that have special meaning and cannot be used as variable names. For example, function
, if
, and return
are all reserved words.
In non-strict mode, it is possible to use reserved words as variable names, but this can lead to confusion and unexpected behavior.
In strict mode, however, using a reserved word as a variable name will result in a syntax error. This helps prevent bugs and makes the code more readable.
Here is an example of how strict mode disallows the use of reserved words:
function exampleFunction() {
"use strict";
const function = "this will cause a syntax error";
}
In this example, the variable name function
is a reserved word, so trying to assign a value to it will cause a syntax error in strict mode.
In conclusion, “use strict” is a valuable tool for modern JavaScript development. By opting in to strict mode, developers can write more secure, reliable, and efficient code. However, it is important to understand the potential downsides of using strict mode and to use it judiciously. By using strict mode in combination with other best practices, such as code reviews, testing, and error handling, developers can create high-quality JavaScript code that is both reliable and easy to maintain.