Why is 0 == ""
true in JavaScript? I have found a similar post here, but why is a number 0 similar an empty string? Of course, 0 === ""
is false.
Asked
Active
Viewed 1.8k times
35

Community
- 1
- 1

Horst Walter
- 13,663
- 32
- 126
- 228
-
You mean 0 is "casted" in false, "" is casted in false and the check is false == false => true – Horst Walter Sep 30 '11 at 01:12
-
For PHP, see http://php.net/manual/en/types.comparisons.php – apscience Sep 30 '11 at 01:13
-
I believe it's because Javascript automagically coerces strings and numbers in some contexts, the == operator is one of those contexts, and "" coerces to 0. – millimoose Sep 30 '11 at 01:14
-
2@HorstWalter No. The sting is coerced to the Number type. See my answer. – Šime Vidas Sep 30 '11 at 01:15
-
@ŠimeVidas: That comment with all the up votes is remarkably different from your answer (which is much better). – user113716 Sep 30 '11 at 01:28
-
@Ӫ_._Ӫ Well that was my first reaction. `:)` – Šime Vidas Sep 30 '11 at 01:32
-
@ŠimeVidas: Ah, so you were thinking logically. Don't forget we're talking about JavaScript here. ;) – user113716 Sep 30 '11 at 01:33
-
1@Ӫ_._Ӫ Yea, I had the idea that a `==` comparison of two falsy values always evaluates to true. But then I remembered `NaN != NaN`... and that whole idea collapsed lol. – Šime Vidas Sep 30 '11 at 01:42
-
@ŠimeVidas: Yep, `NaN` is the one value that will never pass a `==` test (or it will *always* pass a `!=` test). – user113716 Sep 30 '11 at 01:50
1 Answers
74
0 == ''
The left operand is of the type Number.
The right operand is of the type String.
In this case, the right operand is coerced to the type Number:
0 == Number('')
which results in
0 == 0
From the Abstract Equality Comparison Algorithm (number 4):
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
Source: http://es5.github.com/#x11.9.3

Šime Vidas
- 182,163
- 62
- 281
- 385
-
Thanks, I did just expect it the other way round, the 0 converted to string and then false. – Horst Walter Sep 30 '11 at 01:20
-
Yes, on the other hand this means that saying both are falsy - as in the other answers, is not quite correct. Because - as you very well showed - the reason is that "" => 0. Thanks for your support! – Horst Walter Sep 30 '11 at 01:26
-
1@Horst - yes, that's right. Both _are_ falsy, as you can see if you use them alone in `if (0)` or `if("")`, but in the case of an `==` comparison that's not what's happening. (Not sure why Šime also left a comment above saying "Because both value are falsy".) – nnnnnn Sep 30 '11 at 01:29
-
3Ooh, I didn't know about es5.github.com. Much handier than typing out page numbers into the PDF. – millimoose Sep 30 '11 at 01:32
-
-
Which sometimes makes me hate Javascript's conversion system. In a language like R, it is 0 which would be converted to character "0" and the result would be, as expected, false. Converting 0 to "0" does have some logic, but converting "" to 0 doesn't have any logic at all... – Adrian Jan 01 '17 at 14:34