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 stringent mode. This setting allows the environment to have some rigid constraints. Strict mode has different meanings 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 further 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.

In strict mode, some mistakes are thrown; in sloppy mode, these errors go silent. Because it becomes more challenging for JavaScript engines to conduct any optimization when errors are fixed, strict mode codes occasionally execute faster than equivalent sloppy mode codes.

  • 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.

  • Sloppy JavaScript does not deliver an error when a variable is not readable, even when strict mode would have given the developer an error message.

  • When we attempt to assign a value to a getter-only, non-existing, or non-writable property in strict mode, errors will be raised.

Examples

Let us see some examples to understand which are not allowed in strict mode

Source Code

// Assigning to a variable that is not declared beforehand "use strict"; a = 2.5; console.log("Value of variable a = " + a);

Output

a = 2.5;
  ^
ReferenceError: a is not defined
....

So, declare a variable and use it

Source Code

"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);

Output

Value of variable a: 2.5
Value of variable b: 5.7

Deleting variables is not allowed in strict mode.

Source Code

"use strict"; let a = 2.5; console.log(a); delete a;

Output

delete a;
       ^
SyntaxError: Delete of an unqualified identifier in strict mode.
....

Writing to read-only objects is not allowed.

Source Code

"use strict"; const obj = {}; Object.defineProperty(obj, "a", {value:10, writable:false}); console.log(obj.a) obj.a = 5.27;

Output

10
obj.a = 5.27;
^
TypeError: Cannot assign to read only property 'a' of object '#<Object>'
...

Creating a variable with a name same as a reserved keyword is not allowed

Source Code

"use strict"; let public = 15;

Output

let public = 15;
^^^
SyntaxError: Unexpected strict mode reserved word
...

In sloppy mode, we can use ‘With’ keyword which is not allowed in strict mode −

Source Code

// in sloppy mode with (Math){a = sqrt(64)}; console.log(a);

Output

8

Source Code

// in strict mode "use strict"; with (Math){a = sqrt(64)}; console.log(a);

Output

with (Math){a = sqrt(64)};
^^^^
SyntaxError: Strict mode code may not include a with statement
...

Conclusion

JavaScript's strict mode makes it possible to develop the language in a safe environment. This mode restricts the implicit conversion from shoddy modes and forbids codes with poor syntax. JavaScript optimizes the programs to minimize compilation mistakes, however, the level of optimization is substantially lower in Strict mode. Sometimes, tight mode code executes more quickly than equivalent code written in sloppy style.

Arnab Chakraborty
Arnab Chakraborty

Corporate Trainer

Updated on: 04-Apr-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements