-
-
Save mde/1013691 to your computer and use it in GitHub Desktop.
js> isNaN(null); | |
false | |
js> null > -1; | |
true | |
js> null < 1; | |
true | |
js> null > 0; | |
false | |
js> null < 0; | |
false | |
js> null == 0; | |
false |
The main tip with using non-strict equality is ToNumber
conversion. When it's not obvious which result will have the comparison, just start to apply ToNumber
to operands, until you rich the same types, i.e. numbers. Another case -- to use strict ===
equality ;)
P.S.: http://dmitrysoshnikov.com/notes/note-2-ecmascript-equality-operators/
@BrendanEich as always, the history is fascinating - thanks!
this also bothers me:
js> isNaN(null)
false
js> parseFloat(null)
NaN
it makes sense if you read what parseFloat
is supposed to do but still very weird...
@millermedeiros I was confused the same way with parseInt, but I guess it made a little more sense to me that explicit conversions might behave different from implicit coercions. That's why the double-equal thing seemed particularly weird. @BrendanEich Thanks for chiming in with some context and history on this. :)
isNaN converts non-number (typeof sense) arguments to number, null converts to +0. Relational operators convert too. == looks good by comparison here!
Fixes:
/be