🔰comparison operator
<, >, <=, >= operators
Last updated
<, >, <=, >= operators
Last updated
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.