Difference Between == and === Operator in JavaScript

In Javascript, the “==” operator is known as an abstract comparison operator whereas the “===” operator is known as the Strict comparison operator.

What is == Operator in Javascript?

The double equals to is a comparison operator in javascript. It is used to compare two data or variables etc. However, it is not strict while checking the equality of the two objects, variables, etc. It first attempts to convert both objects’ data types to the same data type. Then only it compares the two objects.

For example, suppose we want to compare a number and a string using the == operator, then javascript will internally try to convert the data types of both the object to some common value.

The empty string is converted to zero(0) as the number. On the other hand, a string with no numerical value is converted to an undefined or null value denoted by NaN(It stands for Not a Number).

So if we try to compare an empty string with 0, Javascript will show true as the truth value.

1) The empty string is converted to 0 :

Example

var num = 0;

var st = "";

if(num == st) {

    console.log("Yes they are equal.")

}

else {

    console.log("Sorry they are not equal.")
}

Output

Yes, they are equal.

2) The string or the number not assigned any value is treated as undefined:

a)Example

var num=0;

var st;

if(num==st){

    console.log("Yes they are equal.")

}

else {

    console.log("Sorry they are not equal.")

}

Output

Sorry, they are not equal.

b)Example

var num;

var st;

if(num==st){

    console.log("Yes they are equal.")

}

else{

    console.log("Sorry they are not equal.")

}

Output

Yes, they are equal.

What is === Operator in Javascript?

Triple equal to is another comparison operator in JavaScript. This is strictly used to compare objects, variables, data, etc.. While using the “===” operator, the javascript internally does not attempt to make the data types of both objects equal.

In other words, the operator performs typecasting to compare the data. So it does not consider the empty string equivalent to zero (0). It never returns true as the truth values while comparing with string and number because both are different data types.

Hence 2 and “2” when compared using the “===” operator, return the true value as false.

Example

var num=9;

var st="9";

if(num===st){

    console.log("Yes they are equal.")

}

else{

    console.log("Sorry they are not equal.")

}

Output

Sorry, they are not equal.

Explanation

Although both num and the st variable store 9, they represent different data types; hence, they are treated as unequal by the === operator.

Example

var st="Hello"

if(st==="Hello"){

    console.log("Yes they are equal.")

}

else{

    console.log("Sorry they are not equal.")

}

Output

Yes, they are equal.

Example

var a=34

if(a===34){

    console.log("Yes they are equal.")

}

else{

    console.log("Sorry they are not equal.")

}

Output

Yes, they are equal.

Example

var a=34

if(a==="34"){

    console.log("Yes they are equal.")

}

else{

    console.log("Sorry they are not equal.")

}

Output

Sorry, they are not equal.

The difference between “==”  and “===” in JavaScript

The main difference between the == and the === operator is that the == operator first tries to convert the data types that need to be checked whether they are equal or not. The === operator checks strict equality. It doesn’t attempt to convert both the objects to the same data types.

Example

var a=9;

var b='9';

console.log(a==b);

Output

true

Example

var a=9;

var b='9';

console.log(a===b);

Output

false

That’s it for this tutorial.

Related posts

How to Check If Two Objects are Equal in JavaScript

How to Check If a Variable is an Array in JavaScript

Leave a Comment