References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Less Than < operator

Operator JavaScript All modern browsers Updated
Quick answer

The less-than operator < returns true if the left value is less than the right. 3 < 5 is true. Numbers compare numerically; two strings compare alphabetically by character code, so "10" < "9" is true (surprising for numbers-as-text). Comparisons with NaN are always false.

Overview

< returns true when the left value is smaller than the right. With numbers it behaves exactly as in math — 3 < 5, -1 < 0 — and it's everywhere in loops, conditions and bounds checks.

As with >, the surprise is strings. Two strings compare lexicographically, character by character by Unicode value. So "apple" < "banana" is true, but "10" < "9" is also true — because the character "1" is less than "9", even though the numbers say otherwise. Convert numeric strings with Number() before comparing them as numbers.

For mixed types the operands are coerced to numbers ("5" < 10 is true), and the universal NaN rule applies: any comparison with NaN is false. Its companions are >, <= and >=.

Syntax

a < b

3 < 5             // true
"apple" < "banana" // true  (alphabetical)
"10" < "9"         // true  (string compare!)
"5" < 10           // true  (mixed -> numeric)
1 < NaN            // false

Example

Live example
<pre id="out" style="font:14px ui-monospace,monospace"></pre>
<script>
  const sum = [];
  for (let i = 0; i < 5; i++) sum.push(i);

  document.getElementById('out').textContent =
    'loop:        ' + sum.join(', ') + '\n' +
    '"10" < "9":  ' + ('10' < '9');
  // loop: 0, 1, 2, 3, 4 / "10" < "9": true
</script>

Best practices

  • Convert numeric strings with Number() before comparing as numbers.
  • Remember string comparison is alphabetical (by code point), not numeric.
  • It's the standard loop-bound operator — for (let i = 0; i < n; i++).
  • Any comparison with NaN is false, so validate values that might be NaN.

Frequently asked questions

Why is "10" < "9" true?
Strings compare alphabetically by character code, and "1" comes before "9". Use Number() for numeric comparison.
How does < compare strings?
Character by character by Unicode code point (lexicographic order), not by length or numeric value.
What does < do with mixed types?
It coerces both operands to numbers, so "5" < 10 is true.
What does 1 < NaN return?
false — any relational comparison involving NaN is false.