Introduction
In JavaScript, most of the time the developer will make a mistake by using the equality operator instead of identity while doing a comparison. It looks like both would do a similar job, but actually they work in different ways. In this blog, we are going to see the basic differences between these two operators.
== VS ===
Equality operator (==)
It also called type converting operator because while using this operator for comparison, internally it will convert both the values into a common type before the comparison is done.
Identity operator (===)
Also called strict operator, because while using this operator, it will not do a type conversion of both values. If both the values don’t have the same value and type, it doesn’t take it as equal which means that it will validate both the value and type of the operands.
At this point, I hope everything is clear.
It’s always better to use the Identity operator instead of Equality while doing a comparison since it’s always faster than the equality operator.
Check the result of the below example:
From the above example you can notice that using the equality operator, ‘10’ ==10 will give you the result as true because internally it will typecast to the common type and compare the value, whereas ‘10’===10 will result as false because === operator will not perform a typecast.
Summary
We have seen a basic difference between the identity and equality operator and went through the example which gives you a basic understanding of these operators and how they are working while comparing the value.