Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

How does < compare values of different types, and why is null >= 0 true?

Questions in this chapter (6)
  • Draft
  • medium
  • Askedoften
  • Verified October 2026

Answer

<, >, <= and >= all use one abstract operation, IsLessThan(x, y), which returns true, false or undefined ("not comparable"):

  1. Convert both operands with ToPrimitive(…, number): objects try valueOf before toString.
  2. If both primitives are strings, compare them code unit by code unit; a proper prefix is less. No conversion to numbers happens, so '10' < '9' is true.
  3. A BigInt and a string: parse the string with StringToBigInt; if that fails, the result is undefined.
  4. Otherwise convert both with ToNumeric and compare numerically. A BigInt and a Number compare by exact mathematical value. If either side is NaN, the result is undefined.

The operators then map that result:

  • x < y is IsLessThan(x, y); x > y is IsLessThan(y, x). Undefined becomes false.
  • x >= y is not IsLessThan(x, y); x <= y is not IsLessThan(y, x). Undefined becomes false here too, rather than being negated.

So null >= 0: null converts to 0, 0 < 0 is false, and >= returns the negation, true. null > 0 is false (it asks 0 < 0). Meanwhile null == 0 is false, because loose equality has its own algorithm in which null equals only null and undefined. The relational operators and == were never designed to agree.

undefined converts to NaN, so every relational comparison with it is false, including undefined >= 0.

One subtlety: x > y swaps the operands, so the spec passes a leftFirst flag that still converts x before y, keeping left-to-right order for side effects.

Example

console.log(null >= 0, null > 0, null == 0); // → true false false
console.log(undefined >= 0, undefined < 0); // → false false
console.log(NaN < 1, NaN >= 1); // → false false

console.log('10' < '9', '10' < 9); // → true false
console.log('Z' < 'a', 'apple' < 'apples'); // → true true

console.log([2] > [10]); // → true
console.log(new Date(2026, 0, 2) > new Date(2026, 0, 1)); // → true

console.log(1n < 1.5, 1n < '2', 1n < '1.5'); // → true true false

console.log(1 < 2 < 3, 3 > 2 > 1); // → true false

Pitfalls

  • Comparing numeric strings from inputs or JSON: '10' < '9' is true. Convert with Number() first.
  • Writing a >= b as a stand-in for !(a < b) with NaN or undefined around. Both a < b and a >= b are false when either is NaN.
  • Sorting user-facing text with <. It compares UTF-16 code units, so 'Z' < 'a' and accented letters sort after z; use Intl.Collator or localeCompare.
  • Chaining comparisons like in maths. 3 > 2 > 1 is (3 > 2) > 1, which is true > 1, which is 1 > 1: false.

Follow-ups

How do you compare two arrays or objects with <?
You don't, meaningfully. Plain objects become "[object Object]", so {} < {} is false and {} >= {} is true. Arrays become their joined string, compared as text. Compare the fields you care about explicitly.
Why do Date objects compare correctly with < but not with ==?
< uses the number hint, so Date's valueOf returns the time value and the numbers are compared. == between two objects compares identity, so two equal dates are not ==.
Does a Symbol compare with <?
No. A Symbol survives ToPrimitive and then ToNumeric throws a TypeError.

Exercises

Predict the output

const log = [];
const a = { valueOf() { log.push('a'); return 1; } };
const b = { valueOf() { log.push('b'); return 2; } };
console.log(a > b, ({}) >= ({}), log.join(''));

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.