Sub topic 12
Relational Comparisons and Their Surprises
This chapter assumes you know the coercion steps ToPrimitive, ToNumber, and ToString, and how == handles null. The relational operators <, >, <=, and >= also convert their operands. They follow their own algorithm, though, and it differs from the one == uses.
The comparison algorithm
When you write a < b, JavaScript works through these steps:
- It converts both operands with ToPrimitive, using the "number" hint. Objects call
valueOffirst and thentoString. - If both results are strings, it compares them code unit by code unit, which amounts to lexicographic order.
- In every other case, it converts both sides to numbers and compares them numerically.
- If either side becomes
NaN, the comparison isfalse.
Strings compare by code unit
console.log('10' < '9'); // true: '1' (49) comes before '9' (57)
console.log(10 < '9'); // false: '9' becomes the number 9
console.log('B' < 'a'); // true: 'B' is 66, 'a' is 97
console.log('apple' < 'apricot'); // true: 'p' < 'r' at index 2The first line catches people most often. Values read from form inputs, URL parameters, or CSV files arrive as strings, so comparing them sorts '10' before '9'. Convert them with Number() before you compare.
For text meant for humans, use localeCompare, which handles case and accents according to the locale:
console.log('a'.localeCompare('B')); // -1 in most locales
console.log('10'.localeCompare('9', undefined, { numeric: true })); // 1null and undefined
console.log(null >= 0); // true: null becomes 0
console.log(null > 0); // false
console.log(null == 0); // false: == has a special rule for null
console.log(undefined < 1); // false: undefined becomes NaN
console.log(undefined >= 0); // falseThe relational operators turn null into 0, but == never converts null to a number. That is why null >= 0 is true while null == 0 is false. undefined becomes NaN, so every relational comparison involving it returns false.
Objects and dates
Objects go through ToPrimitive. Because a Date's valueOf returns a timestamp, relational comparison of dates works as expected. Equality does not: == and === compare dates by reference.
const d1 = new Date('2024-01-01');
const d2 = new Date('2024-01-01');
console.log(d1 < new Date('2025-01-01')); // true
console.log(d1 <= d2 && d1 >= d2); // true
console.log(d1 == d2); // false: different objects
console.log(+d1 === +d2); // true: compare timestamps
console.log([2] > 1); // true: [2] -> '2' -> 2Common errors
| Mistake | Fix |
|---|---|
| Comparing numeric strings | Convert them with `Number()` first |
| Sorting names with `<` | Use `localeCompare` |
| Checking dates with `===` | Compare `getTime()` values |
| Assuming `NaN` falls somewhere in the order | Check it first with `Number.isNaN` |
Exercise
What do '25' > '3', '25' > 3, and null <= 0 evaluate to, and why?
Show answer
'25' > '3' is false because both operands are strings and '2' comes before '3'. '25' > 3 is true because one operand is a number, so '25' becomes 25. null <= 0 is true because null becomes 0 and 0 <= 0.
Exercise
Why does undefined <= undefined evaluate to false?
Show answer
Both operands become NaN, and every relational comparison involving NaN returns false.