0 == false //true 0 === false //false
Double Equals (==)
Now for some more examples. For starters, when comparing
false, both evaluate to
'0' == false //true '' == false //true
However, when we compare them against each other:
'0' == '' //false
Interesting. Why is that? This is how the double equals works:
So, with the example at hand, JS evaluates the operand type first. Since neither operand is a number or boolean and at least one of the operands is a string, they are evaluated as strings. (In this case, just so happens both operands are strings.) Next JS evaluates the values, which are different. Despite the “falsey” nature of both zero and the empty string, as string types they are altogether different values and therefore not equal.
Let’s take a look at a couple other intriguing examples.
'' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true ' \t\r\n ' == 0 // true
To explain some of these examples that were perplexing to me the first time I examined them…
false == 'false', JS tries to convert both to numbers. The result is
0 === NaN, which is of course false. This also explains why
false == '0' is true–because it boils down to
0 === 0.
It appears that when
' \t\r\n ' is converted to a number, JS converts it to
0. At any rate
isNaN(' \t\r\n ') results in
false, which tells me that when converted to a number,
' \t\r\n ' does NOT result in
' \t\r\n ' must boil down to
' \t\r\n ' == 0 is true.
Some developers avoid using == in their code because they don’t understand it and it’s confusing. I think it’s important to understand how == works since this will lead to language mastery. Furthermore, if you don’t use ==, your colleagues or 3rd party libraries will, and if you don’t understand it, then you are disempowered. After you understand ==, I see no problem with not using it. But simply avoiding it because it’s confusing and you don’t understand it seems unwise.
If you’re interested in the actual specification: Abstract Equality Comparison Algorithm
If performance is of concern when using == vs ===, these may be worth perusing:
- JSPerf – Equality Operators with Same Types
- JSPerf – Comparison of Comparisons