Summary
-
All comparison operators, including greater/less than, greater/less than or equals, equals, and not equals, return a boolean value, either
true
orfalse
. -
JavaScript uses lexicographical order to do string comparison.
-
When comparing values of different types, JavaScript converts the values to numbers.
-
A regular equality check
==
cannot differentiate0
fromfalse
. This happens because operands of different types are converted to numbers by the equality operator==
. An empty string, just likefalse
, becomes a zero. Thus, we use a strict equality operator===
to check the equality without type conversion. There is also a strict non-equality operator!==
analogous to!=
. -
For a non-strict check
==
,null
andundefined
equals each other, but not any other value. For maths and other comparisons like<
,>
,<=
, and>=
,null
becomes0
whileundefined
becomesNaN
. To sum up, treat any comparison withundefined/null
except the strict equality===
with exceptional care. Don’t use comparisons<
,>
,<=
, and>=
with a variable which may benull/undefined
.
Tasks
-
Comparisons
5 > 4 // true "apple" > "pineapple" // false "2" > "12" // true undefined == null // true undefined === null //false null == "\n0\n" //false null === +"\n0\n" //false