== and triple equals
=== comparison operator. At first glance they seem to do nearly the exact same thing, but there is one major difference between the two that makes triple equals almost always better.
If you prefer to learn visually, check out the video version of this article here.
What Makes Them Different
'1' into a number so that the equality check is comparing two numbers. This would result in the previous code returning true since the number one is equal to the string one after it is converted into a number.
With triple equals this conversion of types does not occur. That means that if we use the same code as before but with a triple equals comparison we will get false
1 === '1'. This is because the string
'1' is not converted into a number and thus when the equality of the number one is compared to the string one it returns false since a string is never equal to a number.
Why Is This Important
It may seem like double equals is the preferred method for equality checks since it does the type conversion for you and more than likely when you are comparing
'1' you meant for them both to be numbers. In practice, though, this is actually a terrible idea.
The problem is that this could lead to many false positives. For example take the following code.
0 == ''
When To Use Double Equals
So it sounds like double equals is terrible and should never be used due to possible false positives, but there is one case in particular that I find double equals to be incredibly useful. That situation is when you need to check for null/undefined values. If you compare null and undefined with doubles equals it will return true, but with triple equals it will return false.
null == undefined // true null === undefined // false
Many times when I am trying to check a variable to see if it is null I also want to check to see if it is undefined at the same time. Because of this, I almost always use double equals to compare against null/undefined since it will return true if my value is null or undefined.
x == null // True if x is null or undefined x === null // Only true is x is null
In 99% of all cases you should use triple equals comparison when comparing two values to avoid false positives, but if you are attempting to check if a vales is null/undefined then using double equals is the preferred method.