David David - 1 year ago 85
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)

//this always alerts no


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download