I saw an exciting and confusing issue. Based on "JavaScript, the good parts" :
'' == '0' // false
0 == '' // true
So, why?!
I saw an exciting and confusing issue. Based on "JavaScript, the good parts" :
'' == '0' // false
0 == '' // true
So, why?!
Simple answer: Because it is.
Advanced answer:
'' == '0'
compares the items as strings, since they're both strings. No type changes needed, just compare. They're blatantly different, so false
.
0 == ''
compares the items as numbers. ''
converts to 0
so they are the same.
But really, who cares?
In the first case, both are of type String
and hence do not equate, as they are compared by their values.
In the second case, the left hand side is a Number
, so there is a conversion occurring, which makes ''
to 0
and hence true
==
results in typecasting and then comparing, if needed. ===
doesn't typecast, so
0 === '' // false