Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
‘strict’ in JavaScript
3.
Entering the ‘strict’ mode
3.1.
Using strict mode for the whole script
3.2.
Using strict mode for a part of the script
4.
Changes of ‘use strict’
4.1.
Changes mistakes into errors
4.2.
Simplifying variable usage 
4.3.
Making JavaScript more secure
5.
Frequently Asked Questions 
5.1.
Is strict mode supported by all the browsers?
5.2.
Is there some way to come out of strict mode in JavaScript?
5.3.
How to enter strict mode in JavaScript? 
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

‘use strict’ in JavaScript

Author Vidhi Singh
0 upvote

Introduction

When we are around our friends in public areas, we behave very casually. But when we enter our school or college or any organizational premise, we tend to become more formal and follow their restrictions. The ‘strict’ mode is similar to such a premise with restrictions in JavaScript.

Let’s learn in-depth about ‘strict’ mode and ‘use strict’ in JavaScript.

Also Read, Javascript hasOwnProperty

‘strict’ in JavaScript

Strict mode in Javascript is a way of imposing restrictions for developers using JavaScript. 

When your code is in strict mode, the casual mistakes that were ignored earlier are handled more ‘strictly’.
Errors that are simply ignored in non-strict mode may now start to throw exceptions. The code is considered to be safer, and it decreases bugs and increases the security and overall performance of the script.

In Javascript, the strict mode was introduced in the ECMAScript 5 version.

Entering the ‘strict’ mode

In order to invoke the strict mode, we simply have to employ the syntax for same, that is, “use strict” just as we write strings.  

While using the strict mode, one crucial thing is that not all browsers support the strict mode. 
Also, browsers that do not support it may also not throw errors as they may take ‘use strict’ as a string. That is why it is generally recommended that you do feature testing for strict mode support before blindly using it(qualities of a good developer!). 
strict mode actually makes semantic changes. So, in case, that browser does not support strict mode, depending on those changes will cause issues and errors in those browsers.

Just a fun fact:  ‘use strict” can also not be written as use strict directly without inverted commas. This is because it is a string that the compiler uses to change the operating context.

Using strict mode for the whole script

In JavaScript, to enable the strict mode for the whole program, we invoke it at the beginning of the entire code.

Let’s see this by enabling the strict mode for a function in JavaScript: 

"use strict";          //invoking strict mode for whole script
//body of the script
You can also try this code with Online Javascript Compiler
Run Code


Here, “use strict” has been used in the start of the program, so the whole script will run in the strict mode.

Using strict mode for a part of the script

In JavaScript, to enable the strict mode for a part of the script, we invoke it inside different elements of JavaScript like functions, classes, and modules.

Let’s see this by enabling the strict mode for a function in JavaScript: 

function example()
{
    "use strict";          //invoking strict mode
    //body of the function
}
You can also try this code with Online Javascript Compiler
Run Code

 

Here, “use strict” has been used at the start of the function definition, and then the whole body has been written which makes the whole function enter the strict mode.

In case, we declare strict mode in the middle of the code and not the beginning, then the lines of code just after that enter the strict mode. 

Something like this:

//lines of code……………………………………(1)
“use strict”                                                          //invoking strict mode
//remaining lines of code…………………………(2)
You can also try this code with Online Javascript Compiler
Run Code


The lines of code (1) are not in the strict variant of the environment, but the remaining lines of code (2) is inside the strict environment. 

For imposing strict mode on modules, the below syntax is followed:

function strict()
{
    //because this is a module, by default it is strict
}
export default strict; 
You can also try this code with Online Javascript Compiler
Run Code

 

Automatically all the content in the modules is in strict mode only. There is no need for explicit usage of strict keywords for that.
In JavaScript classes, all the parts including class declaration, class expressions, and body are strict mode. 

Some browsers do not allow the strict mode in blocks using curly braces { }.

Changes of ‘use strict’

“use strict” brings changes in syntactic and runtime behavior as well.

Let us look at a few of the major categories of changes in JavaScript in detail

Changes mistakes into errors

Earlier, the things that, in JavaScript, were taken as mistakes and ignored, in strict mode, they are converted to errors due to more restrictions. These can be syntactic or run-time errors. 
A very common example of the same is using variables without declaring them. Remember that objects are variables too, so same rule applies to them as well.

Consider the following code: 

example = 16;
You can also try this code with Online Javascript Compiler
Run Code


The above code will simply assign the value 16 to the variable ‘example’ in non-strict mode, without showing any error or throwing any exception. It creates a global variable, but allows working of this code.

Now see this:

“use strict” 
example=16;
You can also try this code with Online Javascript Compiler
Run Code

 

It produces the following error:

But, the same code will throw an exception if the variable ‘example’  has not been declared earlier somewhere (that too Global or in the same block).

This way it prohibits the accidental creation of Global variables.
Another is that assignments can not be made to non-writable variables. Non-writable variables include arguments, NaN, eval (basically all protected variables) and other non-writable properties.  

For example,

var NaN = 42;
You can also try this code with Online Javascript Compiler
Run Code

 

In non-strict mode, this would fail, but silently. 

But the same in strict mode like this:

“use strict”;
var NaN = 42; 
You can also try this code with Online Javascript Compiler
Run Code

 

The above code will generate TypeError exception. 

JavaScript does not allow the 'delete' operator for an unqualified name. In non-strict mode, it just returns a false value, but in strict mode, it throws SyntaxError. 

Instead, the variable should be assigned ‘null’ in order to free the memory in strict mode without any error. Something like this:

“use strict”;
var x = 3;
x = null;            //x is garbage collected 
You can also try this code with Online Javascript Compiler
Run Code


When in strict mode, duplicate names of the parameters of a function or properties of an object are not allowed. 

The following will produce SyntaxError: 

“use strict”;
function add_two(a,a)
{
  return a + a; 
}
You can also try this code with Online Javascript Compiler
Run Code

Simplifying variable usage 

Simplifying variable usage means mapping the variable name to variable value or say, making definition easier. 

Sometimes, this simple mapping of the variable name to its value or definition takes place at run time. When strict mode is invoked, most of such scenarios are eliminated. Thereby, optimizing the whole code. 
One technique used by strict mode to do so is by prohibiting ‘with’. It may map either to a property of the object passed to it, or to a variable in the surrounding scope during runtime, inside the block, exactly which is unknown. 

For example,

var a = 15;
with (obj) 
{
	a;
}
You can also try this code with Online Javascript Compiler
Run Code

 

In non-strict mode, it produces no error.

But in the following, it produces SyntaxError:

“use strict”;
var a = 15;
with (obj) 
{
	a;
}
You can also try this code with Online Javascript Compiler
Run Code

 

It is very ambiguous if ‘a’ inside with is the variable ‘a’ or obj.a. So, it produces an error. 

Making JavaScript more secure

Using strict mode in JavaScript makes it more secure. JavaScript in browsers can access the private information of the user, so such JavaScript must be partially transformed before it is run, to prohibit access to the forbidden feature. This flexibility of JavaScript makes it effectively impossible to do this without a large number of runtime checks. Certain language functions are used so many times that performing runtime checks has a high-performance cost. A few strict mode twists, plus requiring that user-submitted JavaScript be in strict mode and that it be enabled in a certain manner, substantially reduce the need for those runtime checks.

There are several other changes like making a few function properties like eval and arguments simpler, techniques for performance optimization, etc.  
So basically eliminates all the silent errors done by naive developers and makes the JavaScript more restricted and finer for development purposes. 

One last thing! Once we have entered strict mode, there is no way to come out of it. JavaScript does not provide any such syntax. 

You can compile with the help of Online Javascript Compiler for better understanding.

Frequently Asked Questions 

Is strict mode supported by all the browsers?

No, some browsers may not support strict mode. But they do not generate any errors in that case.
 

Is there some way to come out of strict mode in JavaScript?

No, Javascript has no such way. Once, it is invoked it can not be opted out of.
 

How to enter strict mode in JavaScript? 

It is done using  “use strict”. It should be inside the inverted comma “ “.

Conclusion

This article extensively discusses the strict mode in JavaScript. We also explain the different changes introduced in the strict mode. 

We hope that this blog has helped you enhance your knowledge regarding ‘use strict’ in JavaScript, and if you would like to learn more, check out our articles on JavaScript. Do upvote our blog to help other ninjas grow. 

Happy Coding!    

Live masterclass