The Great Equality Debate:  
                                       == vs ===  in JavaScript

The Great Equality Debate: == vs === in JavaScript

Equality Operators

In other languages, there is actually only one equality operator like C++, Java, Dart, Python etc. But JavaScript is special because Javascript has got 2 equality operators.

1. == / / Abstract equality operator

2. === // Strict equality operator

So, what is the difference between abstract equality operator and strict equality operator? In lots of places friends, you will find the definition of these two things absolutely wrong.

You will find that " \== " only checks the value of the operands whereas " === " checks the value as well as the data type. This statement will you find in a lot of places on the internet and this statement is 100% wrong. There is nothing correct about this statement.

So, let's see actually what happened:

From the ECMAScript International document, they have written that (pls carefully read that below bold statement)

  • For Abstract equality operator ( == )

✅ if Type(x) is same as Type(y), that means doubles equals to == also checks types.

  • For Strict equality operator ( === )

if Type(x) is different from Type(y), that means === also checks types.

That means both of them check type.

Lets see difference between both == and ===

For ==

  • it checks the type of both operands

  • if it is the same, then it calls ===

  • if types are not the same, then type conversion occurs (coercion) and then the comparison is done.

For ===

  • it checks the types of both operands.

  • if types are different, it returns false

  • if types are the same, then value comparison happens.

So, If somebody asks you what is difference between doubles equals to (==) and triples equals to (=== ), then say:

  • Doubles equal to (==) do type conversion while triples equal to (=== ) don't do type conversion. This is the major difference between both of them.

Let's see example:

console.log( 1 == "1" );    //  True

So let's see, what double equals to (==) does.

It will first check the type, here one type is number (1) and the other type is string ("1"), so it means types are different.

According to the rule, now it will try to do type conversion. JS will convert this string to a number and again it will do a comparison.

console.log( 1 == "1" );  --->  console.log( 1 == 1 );

Now the types are the same. So if types are the same, we call the \===. So in === if types are the same, then value comparison happens. 1 === 1 --> values are the same, hence the answer is true.

Let's now think again ( last time 😀 )

If we do,

console.log( 1 == "sachin" );  // false

Here, one input is a number and the other is a string. Now we want to convert string to a number, but will you be able to convert "sachin" to a number 😀 ( not possible). So If you will convert "sachin" to a number, it will lead to an invalid number.

And my dear friends, In javascript, representation of invalid number is by NaN. So it will become

console.log( 1 == NaN );   // hence the answer is false.

That's all for today 🙂

This is my first blog. I hope you have understood well. I tried to keep it as simple as I can. Thank you friends.