Sub topic 15
How <, >, <=, and >= Compare Mixed Types
This chapter assumes you know how ToPrimitive, ToNumber, and ToString work, as covered in earlier chapters. Relational operators reuse those operations with one twist. If both operands end up as strings, JavaScript compares them as text. Otherwise it compares them as numbers.
The Comparison Algorithm
For a < b, the language runs these steps:
- Convert both operands with ToPrimitive using the
"number"hint. The left operand is converted first. - If both results are strings, compare them by UTF-16 code units, one character at a time.
- Otherwise, convert both to numbers (or BigInts) and compare numerically.
- If either side is
NaN, the result isfalse.
console.log("10" < "9"); // true: "1" (code 49) is less than "9" (code 57)
console.log(10 < "9"); // false: "9" becomes 9
console.log("a" < "B"); // false: "B" is 66, "a" is 97
console.log([2] > 1); // true: [2] -> "2" -> 2
console.log("abc" < 1); // false: "abc" becomes NaN
console.log("abc" >= 1); // false: NaN makes every comparison false
console.log(null < 1); // true: null becomes 0
console.log(undefined < 1); // false: undefined becomes NaNWhy null >= 0 Is True
The spec does not define <= and >= as "less than or equal." It defines a >= b as "a < b is not true." For null >= 0, JavaScript checks null < 0, which is 0 < 0, which is false. So null >= 0 is true.
Loose equality follows a separate algorithm in which null only equals undefined. That is why null == 0 is false. Mixed with the relational rules, you get null >= 0 and null <= 0 both true while null == 0 is false.
undefined behaves differently because it becomes NaN. The inner comparison returns "undefined" (no answer) instead of false, and the spec turns that into false. As a result, undefined >= 0 is false.
Objects and Dates
Objects go through ToPrimitive with the number hint, so valueOf runs first. Dates return their timestamp from valueOf, so you can compare them directly:
const start = new Date(2025, 0, 1);
const end = new Date(2026, 0, 1);
console.log(start < end); // true: compares millisecond timestamps
console.log(start == new Date(2025, 0, 1)); // false: == compares identityPlain objects become "[object Object]". Comparing two of them is a string comparison of identical text, so < returns false and <= returns true.
Common Errors
| Mistake | Result | Fix |
|---|---|---|
| Comparing numeric strings | `"9" > "10"` is `true` | `Number(a) > Number(b)` |
| Sorting user-facing text with `<` | Uppercase sorts before lowercase | `a.localeCompare(b)` |
| Assuming `>=` implies `==` | `null >= 0` but `null != 0` | Check for `null` explicitly |
| Ignoring `NaN` | Every comparison is `false` | Validate with `Number.isNaN` |
Exercise
Predict each result: "2" > "12", 2 > "12", null <= 0, undefined <= 0, [10] < [9].
Show answer
"2" > "12" is true (string comparison, "2" beats "1"). 2 > "12" is false ("12" becomes 12). null <= 0 is true (null < 0... the spec checks 0 < null, which is false). undefined <= 0 is false (NaN). [10] < [9] is true: both arrays become the strings "10" and "9", and "1" sorts before "9".