I was looking at the first table on http://zero.milosz.ca/, and wanted to understand why, for example, 0 == []
and 0 != {}
. I'm assuming it's because Number([]) == 0
and Number({}) == NaN
. However, that part seems arbitrary. Why is an empty list 0
and empty object a NaN
?
Using Number(some_object)
will use the string representation of the given object. For your examples the string representations are:
js> ({}).toString();
[object Object]
js> [].toString();
js>
The string '[object Object]'
cannot be converted to a number but the empty string ''
can.
To elaborate a bit on ThiefMaster's answer, I've taken a look into ECMAScript's specifications:
When converting a string into a number, a grammar is used for the conversion. In particular, the mathematical value of StringNumericLiteral ::: [empty]
is defined as 0. In fact, it's 0 for any whitespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With