๐ฐcomparison operator
<, >, <=, >= operators
Last updated
Was this helpful?
<, >, <=, >= operators
Last updated
Was this helpful?
JS โฉ statement โฉ expression โฉ operator โฉ relational โฉ comparison
trichotomy law๏ผ a < b, a = b, a > b
doesn't apply to NaNโ
3 < NaN, // falseโ
3 == NaN, // falseโ
3 > NaN, // falseโ
<=
and >=
do not rely on ==
or ===
โ
<=
is defined to be "not >
".
>=
is defined to be "not <
".
with one exception, if either operand is NaN, the result is always falseโ
โน๏ธ type conversion is involved in comparison.
โน๏ธ object -> primitive conversion is involved in comparison.
โน๏ธ comparison favors numbers, add/concate (+) favors strings.
replit๏ผ comparison
11 < 3, // false (numeric comparison)
'11' < '3', // true (string comparison)
// โญ "prefer-number" conversion
'11' < 3, // false ('11' -> 11)
// โญ `NaN` always leads to `false`
'one' < 3, // false ('one' -> NaN)
'one' == 3, // false ('one' -> NaN)
'one' > 3, // false ('one' -> NaN)
// โญ trichotomy law๏ผ `a < b, a = b, a > b` doesn't apply to NaNโ
3 < NaN, // false
3 == NaN, // false
3 > NaN, // false
String.prototype.localeCompare() โญ๏ธ simply calls Intl.Collator
Intl.Collator โญ๏ธ language-sensitive string comparison.