comparison operators (<, >, <=, >=)
In JavaScript, the comparison operators (<, >, <=, >=) compare two values and return a Boolean that says whether the left operand is less than, greater than, at most, or at least the right operand. MDN calls them the relational operators, a group that also includes the keyword operators in and instanceof.
Both operands are first converted to primitives. When both are strings, they’re compared code unit by code unit, using the UTF-16 value of each character. Otherwise, both are converted to numeric values, and a bigint compares directly with a number. If either side ends up as NaN, every comparison operator returns false.
JavaScript Comparison Operators Syntax
Each comparison operator sits between its two operands:
<left> < <right>
<left> > <right>
<left> <= <right>
<left> >= <right>
The four operators give these results for the operands 5 and 3:
| Operator | Name | Example | Result |
|---|---|---|---|
< |
Less than | 5 < 3 |
false |
> |
Greater than | 5 > 3 |
true |
<= |
Less than or equal | 5 <= 3 |
false |
>= |
Greater than or equal | 5 >= 3 |
true |
All four share one algorithm, and <= and >= return the opposite of > and < unless an operand can’t be compared, such as NaN.
JavaScript Comparison Operators Examples
Say a stock check reads two quantities from a CSV file, so both arrive as strings. Comparing them directly compares text, character by character:
> const stock = "10";
undefined
> const reorderLevel = "9";
undefined
> stock < reorderLevel
true
> Number(stock) < Number(reorderLevel)
false
> stock < 9
false
The character "1" sorts before "9", so the string comparison claims ten is less than nine. Converting with Number() fixes it, and so does a single numeric operand, because a string is then converted to a number.
A sensor reading that fails to parse becomes NaN, and a range check against it never passes:
> const reading = Number("n/a");
undefined
> reading < 0
false
> reading >= 0
false
Neither check is true, so code that tests only reading < 0 lets the bad value through. Checking with Number.isNaN() first catches it.
JavaScript Comparison Operators Gotchas
- String comparison follows UTF-16 code units, so
"Zebra" < "apple"istruebecause uppercase letters come first. Locale-aware ordering needs.localeCompare()orIntl.Collator. - The
<=and>=operators are defined as the negation of>and<, sonull >= 0istrueeven thoughnull == 0isfalse. The equality operators have their own rules. - Objects are converted to primitives before the comparison, so two
Dateobjects compare by timestamp, while two plain objects compare as the string"[object Object]".
JavaScript Comparison Operators Use Cases
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026