!(a <= b)

several times recently I've found myself typing something like:

!(x <= 42)

but why not

x > 42

you might ask. The reason is that x might be null, and comparisons with null are always false — null is not greater than anything, and it is not less than anything — ...


you know, I wrote that, and then I decided to do a quick check, and..

null < 0 is false
null > 0 is false
null == 0 is false
null >= 0 is true!

so.. um..

update: Kyle (see comment) is right — JavaScript is coercing null to 0 when using <, >, <= or >=, so "null < 1" is true as well

hm.. well.. I started this post thinking I was doing something clever, but it turns out I'm doing something wrong.. what I was claiming about null does appear to be true of undefined though, e.g., "undefined < 1" is false

No comments:

Post a Comment