JavaScript Comparison Operators

By

Published · Updated

Tagged: JavaScript

Learn how the JavaScript comparison operators less than, greater than and their or-equal variants compare numbers, and how they order strings by Unicode value.

You can use the following operators to compare two numbers, or two strings.

The operation returns a boolean.

const a = 2
a >= 1 //true
a < 1 //false

To check if a number is inside a range, combine two comparisons with &&:

const x = 7
x >= 1 && x <= 10 //true

Don’t write 1 <= x <= 10. JavaScript doesn’t read it like math does, as you’ll see below.

Comparing strings

When both values are strings, JavaScript compares them character by character, using the UTF-16 code unit of each character. For most characters you’ll meet, that’s their number in the Unicode table.

'a' < 'b' //true
'apple' < 'apricot' //true, 'p' comes before 'r'

This gives some results that look wrong at first:

'Z' < 'a' //true, all uppercase letters come before lowercase ones
'10' < '9' //true, '1' comes before '9'
'é' > 'f' //true, accented letters come after 'z'

It’s not the alphabetical order a person would expect. To sort words for humans, use localeCompare(), which follows the rules of a language. I wrote about it in the String localeCompare() method.

['b', 'a', 'C'].sort((a, b) => a.localeCompare(b))
//[ 'a', 'b', 'C' ]

The '10' < '9' case is also why the default sort() puts '10' before '2': it compares strings.

Comparing different types

If the two values are not both strings, JavaScript converts them to numbers and then compares them.

A string compared with a number becomes a number:

'10' < 9 //false, '10' becomes 10

A string that’s not a number becomes NaN. Any comparison with NaN is false, in both directions:

'abc' < 5 //false
'abc' >= 5 //false
NaN <= NaN //false

So a >= b is not always the opposite of a < b. If one of the values can be NaN, check it first with Number.isNaN().

undefined becomes NaN too, so it’s always false:

undefined < 1 //false
undefined >= 1 //false

null becomes 0, and this one is weird:

null >= 0 //true
null > 0 //false
null == 0 //false

null >= 0 is true, but null == 0 is false, because == follows different rules than < and >. See JavaScript equality operators for how == and === work.

Booleans become 1 and 0. This is what breaks chained comparisons:

1 < 2 < 3 //true, by luck
3 > 2 > 1 //false

3 > 2 gives true, then true > 1 becomes 1 > 1, which is false.

BigInt values and numbers can be compared directly:

5n > 3 //true
'3' < 5n //true

Comparing dates

Dates work with < and >, because a date is converted to its timestamp:

new Date('2026-01-01') < new Date('2026-06-01') //true

But == and === don’t, because they check if the two values are the same object:

new Date('2026-01-01') == new Date('2026-01-01') //false

To check if two dates are equal, compare their timestamps:

new Date('2026-01-01').getTime() === new Date('2026-01-01').getTime() //true

Comparing objects and arrays

Objects and arrays are converted to a primitive value first, usually a string. [2] > 1 is true because [2] becomes '2', then 2. It works, but it’s confusing. Compare the values inside them instead, like arr.length or arr[0].

Want me to talk about your product? You can sponsor this site.

Related posts about js: