Skip to content

Instantly share code, notes, and snippets.

@mde
Created June 8, 2011 03:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mde/1013691 to your computer and use it in GitHub Desktop.
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
@DmitrySoshnikov
Copy link

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/

@rwaldron
Copy link

rwaldron commented Jun 8, 2011

@BrendanEich as always, the history is fascinating - thanks!

@millermedeiros
Copy link

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...

@mde
Copy link
Author

mde commented Jun 8, 2011

@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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment