Javascript has the following comparison operators:
<
less than<=
less than or equal>
greater than>=
greater than or equalUse these to compare numbers with numbers or strings with strings.
Strings are compared lexicographically:
There are no “strict” versions of comparison operations. So, if you mix types, JavaScript will go about converting types!
More madness:
The logic(!) behind the madness:
NaN
. Comparing with NaN
always returns false.
null
, false
, and empty string convert to 0
. And, true
converts to the number 1
.Avoid mixed-type comparisons!