Sub topic 16
How <, >, <=, and >= Compare Mixed Types
This chapter assumes you know how ToPrimitive, ToNumber, and ToString work from the earlier chapters. You should also be comfortable running snippets in a browser console or Node.js.
Equality asks whether two values are the same. Relational operators ask a different question: which value comes first? Two kinds of order exist in JavaScript, numeric order and string order. The language picks one or the other based on the operand types after primitive conversion. Most surprising results come from that choice, so this chapter shows exactly when each order applies.
The Working Model
Every relational operator calls one internal routine, which the specification calls IsLessThan. The routine takes two values and returns true, false, or undefined. The undefined result means "these values have no order", which happens whenever NaN is involved. Each operator then turns that three-way answer into a boolean.
The steps are:
- Convert both operands with ToPrimitive using the hint
"number". The left operand is always converted first. - If both primitives are strings, compare them as strings, one UTF-16 code unit at a time.
- If one is a BigInt and the other a string, parse the string as a BigInt. If parsing fails, the result is
undefined. - Otherwise convert both to numeric values (Number or BigInt) and compare them mathematically. If either value is
NaN, the result isundefined.
How Each Operator Uses the Result
Only < is computed directly. The other three operators are defined in terms of it:
| Expression | Computed as | If the result is `undefined` |
|---|---|---|
| `a < b` | IsLessThan(a, b) | `false` |
| `a > b` | IsLessThan(b, a) | `false` |
| `a <= b` | not IsLessThan(b, a) | `false` |
| `a >= b` | not IsLessThan(a, b) | `false` |
So <= really means "not greater than", and >= means "not less than". This has two effects you will see in practice. First, any comparison involving NaN is false, including <= and >=. Second, <= and >= never consult equality, which explains the null oddity covered below.
The swapped operands in > and <= do not change evaluation order. The specification passes a flag that keeps conversions running left to right, so side effects inside valueOf happen in source order.
String Order Is Code-Unit Order
When both sides are strings, JavaScript compares the first code unit of each. If they are equal, it moves to the next pair. A string that is a prefix of the other comes first.
console.log("10" < "9"); // true: "1" (49) is less than "9" (57)
console.log("Z" < "a"); // true: uppercase letters come before lowercase
console.log("apple" < "apples"); // true: a prefix sorts first
console.log("" < "a"); // true
console.log("é" < "f"); // false: "é" is code unit 233
console.log("é".localeCompare("f") < 0); // true: locale-aware orderString order ignores numeric meaning and language rules. To sort numeric strings, convert them with Number() first. To sort human-readable text, use localeCompare or an Intl.Collator.
Mixed Types Fall Back to Numbers
If even one operand is not a string after ToPrimitive, both are converted to numbers. A string next to a number is therefore compared numerically.
console.log(10 < "9"); // false: "9" becomes 9
console.log("10" < 9); // false
console.log(5 < "abc"); // false: "abc" becomes NaN
console.log(5 >= "abc"); // false: NaN has no order
console.log(true > false); // true: 1 > 0
console.log(" 7 " > 6); // true: whitespace is trimmed, giving 7null, undefined, and the >= Paradox
null converts to 0 and undefined converts to NaN. Combine that with the table above and you get these results:
console.log(null < 1); // true: 0 < 1
console.log(null >= 0); // true: not (0 < 0)
console.log(null > 0); // false
console.log(null == 0); // false: == has its own rule for null
console.log(undefined < 1); // false: NaN
console.log(undefined >= 0); // false
console.log(undefined <= undefined); // false
console.log(undefined == undefined); // truenull >= 0 is true while null == 0 and null > 0 are both false. Each result follows its rules correctly. The loose equality algorithm treats null specially and never converts it to a number, while >= converts it to 0 and checks "not less than". Do not read >= as "greater than or equal" when null might be one of the operands.
Objects Go Through ToPrimitive First
Objects are converted with the "number" hint, so valueOf is tried before toString. Arrays and plain objects return themselves from valueOf and end up as strings. Dates return their timestamp.
console.log([2] < 10); // true: [2] -> "2" -> 2
console.log([10] < [9]); // true: "10" < "9" as strings
console.log([1, 2] < [1, 3]); // true: "1,2" < "1,3"
console.log({} < {}); // false: "[object Object]" twice
console.log({} >= {}); // true: not less than
const jan1 = new Date(2024, 0, 1);
const jan2 = new Date(2024, 0, 2);
console.log(jan1 < jan2); // true: timestamps compared
console.log(jan1 <= new Date(2024, 0, 1)); // true
console.log(jan1 == new Date(2024, 0, 1)); // false: different objectsThe [10] < [9] case catches people. Both arrays become strings, so string order applies even though the elements are numbers. The Date pair shows the reverse pattern. <= succeeds because it compares timestamps, but == compares object identity. Use a.getTime() === b.getTime() to test whether two dates are equal.
BigInt and Symbol
BigInt compares with Number by exact mathematical value, without converting either side to the other type. This means comparison is allowed even though arithmetic mixing throws.
console.log(1n < 2); // true
console.log(2n > 1.5); // true
console.log(1n < "2"); // true: "2" parses as 2n
console.log(1n < "1.5"); // false: "1.5" is not a valid BigInt
console.log(10n > "abc"); // false
console.log(10n <= "abc"); // false: no order
try {
Symbol("id") < 1;
} catch (e) {
console.log(e instanceof TypeError); // true: symbols cannot become numbers
}Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| `"100" < "20"` is true | Both operands are strings | Convert with `Number()` before comparing |
| Range check passes for `null` | `null` becomes `0` | Check `value != null` first |
| Check fails silently on bad input | `NaN` makes every comparison false | Validate with `Number.isFinite()` |
| Accented names sort last | Code-unit order | Use `localeCompare` or `Intl.Collator` |
| Two equal dates are not `==` | Identity comparison | Compare `getTime()` values |
Exercise
Predict each result and explain which order (string or numeric) is used: "2" > "12", "2" > 12, [3] > [20], null <= -1.
Show answer
"2" > "12" is true, because both are strings and "2" (50) is greater than "1" (49). "2" > 12 is false, because one operand is a number, so 2 > 12 is compared numerically. [3] > [20] is true, because both arrays become strings and "3" > "20" by code units. null <= -1 is false, because null becomes 0 and the expression means not (-1 < 0), and -1 < 0 is true.
Exercise
A function guards input with if (!(qty < 1)) { ship(qty); }. Which inputs ship incorrectly, and how should the guard be written?
Show answer
Any input that converts to NaN, such as "abc", undefined, or {}, makes qty < 1 false, so the negation is true and the order ships. Strings like "5" also pass without being converted. Convert and validate first: const n = Number(qty); if (Number.isInteger(n) && n >= 1) { ship(n); }.
Exercise
Why is {} >= {} true while {} > {} and {} == {} are both false?
Show answer
Both objects become the string "[object Object]". {} > {} asks whether the right string is less than the left, which is false because they are equal. {} >= {} is defined as not ({} < {}), and since {} < {} is false the result is true. {} == {} compares object identity, and the two literals are different objects.