Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to enable a strict mode in javascript?
In JavaScript, there are two different programming paradigms. The sloppy mode, sometimes referred to as the simple mode, is activated by default. We don't have to write the code strictly according to guidelines in this manner. On the other side, there is also the strict mode. This setting allows the environment to have some rigid constraints. Strict mode has different semantics than regular code and is not a subset of sloppy mode. This article will explain how to enable strict mode in JavaScript and will also go over some of the features of "Strict" Mode.
Syntax
Different scopes can allow strict modes. The following are the syntaxes:
Strict mode for scripts: To convert an entire script as strict
'use strict';
Strict mode for function: To convert a function as strict
function function_name() {
'use strict';
// function body
}
JavaScript classes are already in strict mode, we do not need to write anything for them.
What are the changes that come after enabling Strict Mode in JavaScript?
In sloppy JavaScript, the faults are occasionally hidden and rendered silent while executing. For instance, we can assign a value to an undeclared variable if we do not define it first. After that initialization statement, a global variable will be generated automatically. But because we are giving a value to an undefined variable, strict mode will produce an error. The following are a few key changes:
Some faults that strict mode throws become quiet errors in sloppy mode.
Because JavaScript engines find it more challenging to optimize strict mode codes, they occasionally execute faster than equivalent sloppy mode ones.
We can develop secure JavaScript code with the aid of strict mode.
It's possible that sloppy JavaScript code secretly accepted incorrect syntaxes. Which, when run in strict mode, can result in an actual mistake. It facilitates the writing of better JavaScript code.
When we attempt to assign a value to a getter-only, non-existing, or non-writable property in strict mode, errors will be raised.
Example 1: Assigning to Undeclared Variables
In strict mode, assigning to an undeclared variable throws an error:
// Assigning to a variable that is not declared beforehand
"use strict";
a = 2.5;
console.log("Value of variable a = " + a);
a = 2.5; ^ ReferenceError: a is not defined
So, declare a variable and use it:
"use strict";
var a;
a = 2.5;
var b = 5.7;
console.log("Value of variable a:", a);
console.log("Value of variable b:", b);
Value of variable a: 2.5 Value of variable b: 5.7
Example 2: Deleting Variables
Deleting variables is not allowed in strict mode:
"use strict"; let a = 2.5; console.log(a); delete a;
delete a;
^
SyntaxError: Delete of an unqualified identifier in strict mode.
Example 3: Writing to Read-Only Properties
Writing to read-only objects is not allowed:
"use strict";
const obj = {};
Object.defineProperty(obj, "a", {value:10, writable:false});
console.log(obj.a);
obj.a = 5.27;
10 obj.a = 5.27; ^ TypeError: Cannot assign to read only property 'a' of object '#<Object>'
Example 4: Reserved Keywords
Creating a variable with a name same as a reserved keyword is not allowed:
"use strict"; let public = 15;
let public = 15; ^^^ SyntaxError: Unexpected strict mode reserved word
Example 5: The 'with' Statement
In sloppy mode, we can use 'with' keyword which is not allowed in strict mode:
// in sloppy mode
with (Math){a = sqrt(64)};
console.log(a);
8
// in strict mode
"use strict";
with (Math){a = sqrt(64)};
console.log(a);
with (Math){a = sqrt(64)};
^^^^
SyntaxError: Strict mode code may not include a with statement
Key Benefits of Strict Mode
| Feature | Sloppy Mode | Strict Mode |
|---|---|---|
| Undeclared variables | Creates global variable | Throws ReferenceError |
| Delete variables | Silently fails | Throws SyntaxError |
| 'with' statement | Allowed | Throws SyntaxError |
| Reserved keywords | May allow as variables | Throws SyntaxError |
Conclusion
JavaScript's strict mode makes it possible to develop the language in a safe environment. This mode restricts implicit conversions and forbids codes with poor syntax, helping catch common programming mistakes early and making your code more secure and maintainable.
