Table of contents
Hi Everyone, This is my first blog and I have tried to write it very confidently. I have visited many sites and watched many youtube videos for this differentiation but all the content is very confusing. So I think let's summarize this in a very simple way. Hope you will understand it!...
As we know we have some comparison operators in js for comparing the values like >,<,>=,<=,&& or ||, etc... We have also these two operators (== and ===) with them for comparison. So using these operators you can get true or false in return but both have different rules to follow. That is the main topic of my blog to discuss what is the main difference between them?
The == Operator:
So first I will talk about the == operator which is also known as the abstract equality operator because it does the necessary type conversion before doing the equality comparison. And the whole process of converting data type for comparison is also known as Type Coercion. It is also known as a loosely equality operator because this operator is mainly used to compare the value only after converting the data type then it will return a true or false on the basis of comparison. So, If I talk about the examples let's see below:
console.log(2=="2") // It will return true because first it will convert the striing value to number.
console.log("Hello Hashnode" == "Hello Hashnode")// it also returns true
console.log(true==1)// here 1 will convert to true. So it will return true.
console.log(false==0)// here 0 will convert to False so it will return True.
console.log("Hello HashNode"== new String("Hello HashNode")// True because here first string object will convert into string literal.
console.log(null == undefined)//TRUE Because this is the special case where null is a primitive data type so it will return True even both lie in the false case and also contains an empty value or undefined. THIS IS THE WONDER OF JS..๐
// some FALSE Cases...
console.log(null == false)// Any GUESSES GUYS! LEt me tell you here is the same logic will appply that null is a primitive data so it will never be compared to a boolean data type so it will return FALSE...
console.log(4=='5')// FALSE here even after the type conversion the value is different so it will return false.
console.log(true == 0)// FALSE same logic of previous one.
console.log(false==1)// FALSE same logic of previous one.
The === Operator:
The === operator is also known as Strict Quality Operator because as the name suggests it will not do the type conversion so it will return false directly if the values are not of the same type. So it means it directly compares the values and dataTypes instead of doing type conversion first. If the type and value match it will return true directly otherwise False if any one condition will compromise. You can understand with these examples:
console.log(2==="2") //FALSE because here is no type conversion takes place.
console.log("Hello HasNode" === new String("Hello HashNode")// False because no type conversion takes place.
console.log(true === 1) // False again because the reason is same here too...
console.log(false === "0")// False
console.log(true ==='true')// False
console.log(2==='2') // False
// Some TRUE Case..
console.log(5===5)// true
console.log(true===true) // true
console.log("hello HashNode"=== "hello HashNode")// True
console.log(false === false)// True
So, as you see there is the basic difference between them is that the abstract equality operator will do a type conversion first and then do a comparison between the operands whereas the strict equality operator will not do the type conversion at all. It directly compares the type and values of the operands if both are the same then it will return TRUE otherwise FALSE.
I hope you will understand the basic difference between them. ThankYou!!