Strict mode in JavaScript

JavaScript Strict Mode: In this tutorial, we are going to learn about the strict mode in JavaScript.
Submitted by Siddhant Verma, on November 22, 2019

JavaScript Strict Mode

Here, we will explore what strict mode is in JavaScript?

To practice code without installing JavaScript compiler in your system: Open the chrome dev console to try out the examples by right clicking on the browser → selecting inspect → selecting console or simply type f12.

Let's start with a simple operation,

const a = 20;
console.log(a == 20);

Output

true;

We know that there are two comparison operators in hand for us in JS. The double equal (==) to and the triple equal (===) and we also know the difference between them. However, there is a common terminology associated with both which also marks as a distinguishing feature. The double equal to operation is often referred to as "loose comparison" and the triple equal to as "strict comparison".

const a = 20;
console.log(a == '20');

Output

true;

Wait what just happened? We compared a number to a string and we got a truthy. The reason behind this is the "loose comparison" happening under the hood. JS evaluates the string and says okay this says 20 and we're comparing it to a number whose value is also 20 and since they both represent the same value let's return true. If we used a triple equal (===) to operator or strict comparison,

const a = 20;
console.log(a === '20');

Output

false;

Now we get false because of a strict comparison between the types.

Based on this concept JavaScript has an in built strict mode which when used, strictly checks the code for any kind of errors. Some of the flexibility that is usually allowed in the language is barred in the strict mode so that the user writes pure, clean error free code thereby preventing bugs from occuring in the first place. Let's look at some examples,

function print() {
    for (i = 0; i < 3; i++) {
        console.log(i);
    }
}

Output

0
1
2

Your code runs perfectly fine producing the correct output even though it had an error. Variable i was not declared anywhere and directly initialized and used. JavaScript reads your code and understands that you must have forgotten to declare i so it does that for you while executing the code. However, running the same code in strict mode.

function print() {
    "Use strict";
    for (i = 0; i < 3; i++) {
        console.log(i);
    }
}

Output

ReferenceError: i is not defined.
function person(name) {
    this.name = name;
}
let fuzzy = person("Fuzzy");

Can you guess what's wrong with the above code? We have omitted the new keyword before person("Fuzzy") while declaring an object of person type. However, this code runs perfectly fine without producing any errors.

But if we run this in strict mode,

"use strict";

function person(name) {
    this.name = name;
}

let fuzzy = person("Fuzzy");

Output

TypeError: Cannot set property of 'name' undefined.

Strict mode disallows giving a function multiple parameters with the same name and removes certain problematic language features.

JavaScript Tutorial »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.