javascript interactive

JavaScript Interactive — a console crash course


1.6 Numeric Comparison

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.
>>>
1 < 1
false
The numeric comparison operators (<, <=, > and >=) work as expected...
>>>
42 <= 42
true
>>>
4711 >= "4711"
true
Numeric comparison will coerce both operands to numbers.
>>>
NaN <= NaN
false
The special NaN (Not A Number) value behaves in a different way.
>>>
isNaN(NaN)
true
Using the built-in function isNaN, the special case can be checked.
>>>
+ "  42  "
42
With the unary prefix + operator, any value can be coerced into a number.
>>>
+ null
0
Some results might be expected...
>>>
+ undefined
NaN
...others not so much
>>>

Index 1 2 3 4 5 6 7