When both operands are numeric, they are compared normally:
1 < 2 // true
2 <= 2 // true
3 >= 5 // false
true < false // false (implicitly converted to numbers, 1 > 0)
When both operands are strings, they are compared lexicographically (according to alphabetical order):
'a' < 'b' // true
'1' < '2' // true
'100' > '12' // false ('100' is less than '12' lexicographically!)
When one operand is a string and the other is a number, the string is converted to a number before comparison:
'1' < 2 // true
'3' > 2 // true
true > '2' // false (true implicitly converted to number, 1 < 2)
When the string is non-numeric, numeric conversion returns NaN
(not-a-number). Comparing with NaN
always returns false
:
1 < 'abc' // false
1 > 'abc' // false
But be careful when comparing a numeric value with null
, undefined
or empty strings:
1 > '' // true
1 < '' // false
1 > null // true
1 < null // false
1 > undefined // false
1 < undefined // false
When one operand is a object and the other is a number, the object is converted to a number before comparison.So null
is particular case because Number(null);//0
new Date(2015)< 1479480185280 // true
null > -1 //true
({toString:function(){return 123}}) > 122 //true