References

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

The JavaScript Greater Than > operator

Operator JavaScript All modern browsers Updated
Quick answer

The greater-than operator > returns true if the left value is greater than the right. 5 > 3 is true. With numbers it compares numerically; with two strings it compares alphabetically (by character code), which is why "10" > "9" is surprisingly false. Any comparison with NaN is false.

Overview

> compares two values and returns true if the left is greater. For numbers it's exactly what you expect: 5 > 3, 2.5 > 2. It's a staple of conditions, sorting and range checks.

The catch is strings. When both operands are strings, JavaScript compares them lexicographically — character by character, by Unicode code point — not numerically. So "b" > "a" is true, but "10" > "9" is false, because the character "1" comes before "9". If you're comparing numeric strings (from form inputs, for instance), convert them with Number() first. (Uppercase letters also sort before lowercase; for human-friendly ordering use localeCompare().)

With mixed types (a number and a string), JavaScript coerces to numbers, so "10" > 9 is true. And one rule covers all relational operators: any comparison involving NaN is falseNaN > 1, 1 > NaN and even NaN > NaN are all false. Its siblings are <, >= and <=.

Syntax

a > b

5 > 3         // true
"b" > "a"     // true  (alphabetical)
"10" > "9"    // false (string compare: '1' < '9')
"10" > 9      // true  (mixed -> numeric)
NaN > 1       // false

Example

Live example
<pre id="out" style="font:14px ui-monospace,monospace"></pre>
<script>
  document.getElementById('out').textContent =
    '5 > 3       -> ' + (5 > 3) + '\n' +
    '"10" > "9"  -> ' + ('10' > '9') + '\n' +
    'Number compare -> ' + (Number('10') > Number('9'));
  // true / false / true
</script>

Best practices

  • Convert numeric strings with Number() before comparing, or string comparison will mislead you.
  • Remember two strings compare alphabetically (by code point), not numerically.
  • For human-friendly string ordering, use localeCompare().
  • Guard against NaN — every comparison with it is false.

Frequently asked questions

Why is "10" > "9" false in JavaScript?
Two strings are compared alphabetically by character code, so "1" (the first character of "10") is less than "9". Convert to numbers with Number() for numeric comparison.
How does > compare strings?
Lexicographically — character by character by Unicode code point. Uppercase letters sort before lowercase ones.
What happens when comparing a number and a string with >?
They're coerced to numbers, so "10" > 9 is true.
What does NaN > 1 return?
false. Any relational comparison involving NaN is false.