Double Equals (==) Vs Triple Equals(===) in JavaScript

It doesn't feel much of a difference while looking at Double Equals(==) and Triple Equals(===) while coding in JavaScript. But let me tell you something, there is a significant difference between these two. Both of these are used in equality testing. It seems a common source of confusion for those who are new to javascript is whether to use == or === when making an equality comparison. Here are the differences. 

Double Equals (==)

Double equals(==) are known as the abstract equality comparison operator. That means it will compare the values between a number and a string and if those values match, it will show as "True".

null

Here, 10 is a number and “10” is a string. The double equals(==) are comparing the values between those two and as the value is 10 the result is displaying as “true”.

 

Triple Equals (===)

Triple equals (===) are known as the strict equality comparison operator. That means it will compare the values as well as data types between a number and a string and if and only if the data types, as well as values, are same it will show as “true”, otherwise the result will be displayed as "false".

null

Here, 10 is a number and “10” is a string. The triple equals(==) are comparing the values as well as the data types between those two, and though the value is same, the data type isn't. So the result is being displayed as false. 

 

null

Here "10" and "10" are both strings and their value is also same, so the result is coming as "true".

 

Conclusion 

Now I hope you have properly understood the differences between Double equals(==) and Triple equals(==) in JavaScript. 

In a nutshell, Double equals(==) compare the value and Tripe equals(===) compare the value as well as the data types. 

Personally, I would always suggest you to use the strict comparison operator i.e. Triple Equals(===).

Trust me, Triple equals help you see the code formation more clearly. 

Enjoy. 

You are surfing this article at webtutsplus.com

 

 

 

 

Proudly hosted on: DigitalOcean