David David - 4 months ago 9
Javascript Question

Why isn't undefined less than 1?

So in most cases I've been able to use something similar to these lines, but Javascript has given me this weird result.

If I take some value and it turns out to be undefined, when compared to an integer, it does not appear to be less than or greater than any number. Why is this?

if(undefined < 1 || undefined >= 1)
alert("yes");
else
alert("no");

//this always alerts no


JSFiddle

Answer

There is no operator '<' can not be applied to type 'undefined' error in JavaScript as you would find in other typed languages. As such, JavaScript evaluates incompatible types with an operator to false.

Comments