Might be a little tangential, but I'd like to introduce null
into this topic to demonstrate an elusive pitfall.
When using <
, <=
, >
, >=
with null
or undefined
, the result is generally considered to always be false
. However, there's one exception: Infinity
console.log(undefined < Infinity); // false, as expected
console.log(null < Infinity); // true, surprise!
Also notably, this also applied to the string "Infinity"
console.log(undefined < "Infinity"); // false
console.log(null < "Infinity"); // true, surprise again!
console.log(null < "infinity"); // false, it's case-sensitive
I discovered this weird behavior accidentally, and have no idea why the hell JavaScript behaves this way. But anyway, like it or not, this is JavaScript.
Hopefully, this may help someone one day.