Strict Mode In JavaScript

Akshat Chaturvedi
Last Updated: May 13, 2022

Introduction

 

“STRICT MODE”!! Did you ever hear about this? You might have seen this written somewhere on StackOverflow, or your friend might have told you that it was asked in his Web Development interview!!

 

Whatever it might be, I know that it’s a fairly new term for you, so let’s jump straight to the question you have in mind.

 

What is JavaScript Strict Mode?

 

As the name suggests, Strict Mode enforces some strict principles for a JavaScript program; this means that the leverages given by JavaScript are not available while working in the strict mode.

 

Working in the Strict Mode means that you cannot get away by writing a bad syntax code. JavaScript will avoid simple syntax errors and execute the code flawlessly if you are not working in strict mode.

 

For instance, if you initialize a variable with some value without declaring the variable in the script, the JavaScript compiler will not throw an error and simply ignore it.

 

In other words, the machine understands that a human is not a machine, and it makes errors.

 

Caption: Wholesome robot girl

Source: Reddit

 

But if you want to make errors and learn from your mistakes like a true leader, then JavaScript Strict Mode is made for you.

 

JavaScript Strict Mode got introduced in ECMAScript version 5 (2009), along with it many other features like JSON support, String.trim() function, Array.isArray() function, Array iteration methods, etc. were also got added in ECMAScript version 5.

 

To use JavaScript Strict Mode in your programs, you just have to include one line at the top of your script.

 

"use strict";

// Rest of the program

 

You can use javascript strict mode in two ways- if you declare it at the top of the script, then it will be applied for the entire program, and if you declare this in a function body, then the scope of strict mode will be local meaning it will be applied to only the function.

 

function demo(){
"use strict";
console.log("JavaScript Strict Mode demo");
}

 

Let’s learn from an Example.

 

index.js

function multiply(a, b){
m = a*b;
return m;
}
console.log(multiply(10, 22));

 

As you can see in my index.js file, I have not declared the ‘m’ variable, which is a syntax fault.

 

Let’s see what output I get from this.

 

Caption: Console output without strict mode

 

In my console, why am I getting the correct result? Yes, you guessed it right!! It’s because the JavaScript compiler ignores these petty issues, so we don’t have to worry about it.

 

Now let’s see what happens when I use strict mode.

 

index.js

"use strict";  // working in strict mode
function multiply(a, b){
m = a*b;
return m;
}
console.log(multiply(1022));

 

Output:

Caption: Console output with strict mode

 

Now, Why am I getting an error that m is not defined? Yes, Exactly, you guessed it right again!! Since we are working in Strict Mode, JavaScript doesn’t ignore our errors and throws them on the console.

I’ll have to syntactically correct my code now (that is, declare m before using it)

 

index.js

"use strict"// working in strict mode
function multiply(a, b){
var m = a*b;
return m;
}
console.log(multiply(1022));

 

This code will not give any error and prints 220 on the console.

 

I know you guys must be wondering that if the Strict Mode is so cruel and punishes us for making mistakes, why is it even there, and why do we have to include this in our programs.

 

Why do we need JavaScript Strict Mode?

 

We use strict mode in JavaScript because it identifies the silent errors that were earlier ignored and throws them on the console to correct our syntax. Ignoring such minor errors can give rise to significant mishaps in industry projects.

 

A code written in Strict Mode executes faster because the compiler doesn’t have to deal with minor errors.

 

Till now, we have seen how Strict Mode dealt with the undeclared variable. Now let’s explore more uses of Strict Mode.

 

Use-case of JavaScript Strict Mode

 

For undeclared variables

In the above example, I have explained how the Strict Mode throws errors in the case of undeclared variables. So, you should always correctly declare all the variables in JavaScript before initialization.

 

For undeclared objects

Since the objects are also variables in JavaScript, initializing an undeclared object is also syntactically wrong in JavaScript.

 

"use strict";
obj = {k1:100, k2:200, k3:300, k4:400};

 

This will throw an error that “obj is not defined.”

 

For parameters with the same name in a function

If there are multiple parameters with the same name in a function, we’ll not get an error in the normal mode because the JavaScript compiler simply uses one of the parameters. But in Strict mode, it will give an error because it doesn’t know which parameter to use.

 

"use strict";
function multiply(a, a){
var m = a*a;
return m;
}
console.log(multiply(1022));

 

Here my function doesn’t understand which ‘a’ I want to square. Hence it will randomly give either 100 or 484 in normal mode, giving an error in Strict Mode.

 

For deleting functions or objects

In JavaScript deleting an object or a function is prohibited. So in the strict mode, we should keep this mind that we cannot delete objects and functions in JavaScript.

 

"use strict";
function multiply(a, b){
var m = a*b;
return m;
}
let v = 0;

delete v; // Line 1
delete multiply; // Line 2

 

Line 1 or line 2 will throw an error “Delete of an unqualified identifier in strict mode” on the console.

 

For undeletable properties

Apart from custom-defined objects and functions, many default functions and objects are undeletable like JavaScript Prototypes<LINK TO BLOG - Inheritance and The Prototype Chain in Javascript>.

 

"use strict";
delete Object.prototype;

 

The above delete statement will throw an error saying that we “cannot delete property prototype of a function.”

 

For keywords in JavaScript

Certain keywords cannot be used as variables in JavaScript, so if we accidentally name a variable that matches the name of some keyword, we’ll get an error message in the Strict Mode.

For example, the keyword ‘eval’ or ‘arguments’ cannot be used as variables.

 

"use strict";
let eval28;
let arguments28;

 

This will print an error “Unexpected eval or arguments in strict mode” on the JavaScript console.

 

Frequently Asked Questions

 

What is JavaScript Strict Mode?

JavaScript Strict Mode is a method to switch to a more secure and cleaner version of JavaScript wherein the JavaScript compiler does not ignore the minor syntactical errors.

 

What is the syntax of declaring Strict Mode in JavaScript?

To use JavaScript Strict Mode, we can either include “use strict”; on the top for our script with a global scope or put “use strict”; at the top for our function with a local scope limited to the function body.

 

What is the advantage of using the JavaScript Strict Mode?

The main advantage of working in strict mode is that it identifies the silent errors that were earlier ignored and throws them on the console so that we can correct our syntax.

 

What is the list of keywords that cannot be used as variables in JavaScript?

Some keywords that cannot be used as variables in javascript are:

implements, interface, let, package, private, protected, public, static, and yield.

 

When was Strict Mode got introduced in JavaScript?

Strict mode came into play with the release of ECMAScript version 5 in 2009.

 

Key Takeaways

 

Congratulations!! You have successfully completed the Strict Mode Challenge of JavaScript. Now, you know what JavaScript Strict Mode is and what are the advantages of using it. It is always a good practice to use strict mode in your JavaScript projects.

 

You can refer to some of my previous blogs to get a better understanding of basic JavaScript concepts.

 

Modules in JavaScript <Link>

Functions in JavaScript <Link>

Inheritance and The Prototype Chain in Javascript <Link>

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think