Strict Mode in JavaScript is a restricted variant or semantical stricter that generates errors for the issue, which the engine might handle silently. In this Mode, the deprecated JavaScript features are also caught up, and the related error will be then shown as output. As a result, Strict Mode enhances security, minimizes bugs, and boosts your JavaScript application’s performance.
This write-up discusses Strict Mode in JavaScript along with suitable examples. So, let’s start!
How to enable Strict Mode in JavaScript
By utilizing the “use strict” string, you can enable “Strict Mode” and add it to the global scope for the entire script or apply it to a single function only.
We will now practically go through both of the mentioned ways.
Enabling Strict Mode for whole script in JavaScript
If you want to enable Strict Mode for the whole script, then add the “use strict” string at the start of the script, and write out the required code in the following way:
// other script code
x = 5;
console.log(x);
After executing the above script, we have got a “ReferenceError” because, in Strict Mode, you cannot use variables without defining them:
Enabling Strict Mode for a single function in JavaScript
To enable “Strict Mode” within a function, add the “use strict” string at the function definition’s beginning.
For instance, in the below-given script, we activated “Strict Mode” only for the “displayInfo()” function:
console.log(x);
function displayInfo() {
'use strict';
y = "linuxhint";
console.log(y);
}
displayInfo();
Output
In the given output, you may have noticed that enabling Strict Mode has applied some restrictions to the script, resulting in some errors. To know more about the “not allowed” kind of restrictions within the JavaScript Strict Mode, have a look at the upcoming section.
Undeclared variables within Strict Mode in JavaScript
In JavaScript Strict Mode, a variable cannot be initialized or used as an undeclared variable.
Example: Undeclared variable with Strict Mode in JavaScript
x = 'linuxhint';
We have not declared the “x” variable in the above-given example. So, initializing it with the value “linuxhint” will throw the following “ReferenceError”:
Undeclared objects within Strict Mode in JavaScript
Similarly, in Strict Mode, all added objects must be declared first. In case you are assigning properties and their respective values for an undeclared object, the JavaScript engine will throw a “ReferenceError”:
Example: Undeclared objects with Strict Mode in JavaScript
employee = {name: 'Alex', age: 33};
Output
Deleting objects within Strict Mode in JavaScript
JavaScript does not permit you to delete objects in Strict Mode. If you do so, a SyntaxError will be thrown in the console window.
Example: Deleting objects within Strict Mode in JavaScript
let employee = {name: 'Alex', age: 33};
delete employee;
Output
In “Non-Strict” Mode, the attempt to delete the created “employee” object will fail silently the the “delete employee” expression will be evaluated as “false”:
Duplicating Parameters within Strict Mode in JavaScript
A function declaration in Strict Mode must have unique parameters name as duplicating the parameter name will throw a SyntaxError.
Example: Duplicate Parameters within Strict Mode in JavaScript
In the below-given example, we have defined a “showInfo()” function that comprises duplicate parameters (a, a):
functionshowInfo(a, a) { console.log('Strict Mode in JavaScript')};
showInfo();
Execution of the provided code will throw a “SyntaxError: Duplicate parameter name not allowed in this context”:
Read-only property within Strict Mode in JavaScript
Strict Mode restricts the writing access of a read-only property.
Example: Read-only property within Strict Mode in JavaScript
For instance, we will declare an “object1” having a “y” property in Strict Mode and then set its “writable” property to “false”. Upon doing so, the “y” property of “object1” is converted into read-only or non-writable property:
let object1 = {};
Object.defineProperty(object1, 'y', { value: 12, writable: false });
Next we will assign a new value to the “object1.y” property:
Assigning value to the read-only “object1” property displays a TypeError:
Octal literals within Strict Mode in JavaScript
Numerals that comprise “zero” as a prefix are called Octal literals, and they are not allowed in JavaScript Strict Mode.
Example: Octal literals within Strict Mode in JavaScript
The following example will create an octal literal “x” with the value “001”:
let x = 001;
As mentioned earlier, the strict does not permit to define Octal literals, so you may encounter the following SyntaxError:
Instead of utilizing “0” as a prefix, adding “0o” will solve the issue:
let x = 0o01;
console.log(x);
Now, we have successfully created an Octal literal within the Strict Mode using the “0o” prefix:
eval and arguments within Strict Mode in JavaScript
In Strict Mode, you cannot utilize “eval” and “arguments” as function names, variable names, or function parameters. Both “eval” and “arguments” are treated as “keywords” within the JavaScript Strict Mode.
Example: eval and arguments within Strict Mode in JavaScript
let eval = 32;
let arguments = 'linuxhint';
Using “eval” and “arguments” as variable names will output the following SyntaxError:
Non-extensible object within Strict Mode in JavaScript
The Strict Mode in JavaScript does not allow adding new properties to a created non-extensible object.
Example: Non-extensible object within Strict Mode in JavaScript
Firstly, we will declare an “object3” and convert it to a non-extensible object by utilizing the “Object.preventExtensions()” method. After that, we will attempt to add a “newProperty” to “object3”:
let object3 = {};
Object.preventExtensions(object3);
object3.newProperty = 'my object';
The given output signifies that the created object is not extensible; therefore, we cannot add a new property:
Why use Strict Mode in JavaScript
Here is the list of some of the fantastic advantages of using Strict Mode in JavaScript:
- Strict Mode assists in writing a secure and cleaner script.
- It eliminates the function of hiding silent JavaScript errors by throwing an error statement.
- Fixing faults with the help of JavaScript Strict mode optimizes the code and the JavaScript engine executes it quickly compared to the code written in non-strict Mode.
We have provided the essential information related to Strict Mode, its advantages, and restrictions in JavaScript.
Conclusion
By utilizing the “use strict” string, you can enable Strict Mode in JavaScript in the global context of a script or for a single function. It improves JavaScript semantics by providing extra security. When Strict Mode is enabled in a script, undeclared variables and objects cannot be used. It does not permit adding duplicate parameters, Octal literals, deleting objects, and writing into read-only object’s property. This write-up explained Strict Mode in JavaScript along with appropriate examples.