Why is not undefined less than 1?

Therefore, in most cases, I was able to use something similar to these lines, but Javascript gave me this strange result.

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

if(undefined < 1 || undefined >= 1) alert("yes"); else alert("no"); //this always alerts no 

Jsfiddle

+6
source share
2 answers

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

+8
source

This is how javascript works. If either side cannot be converted to a number (lines can, by comparing the code points: '2' < '3' && '186' < '4' ), then comparing the numbers will return false (works with > , < , <= , >= ).

+2
source

All Articles