Less than (<)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Theless than (<) operator returnstrue if the left operand is less than the right operand, andfalse otherwise.
In this article
Try it
console.log(5 < 3);// Expected output: falseconsole.log(3 < 3);// Expected output: false// Compare bigint to numberconsole.log(3n < 5);// Expected output: trueconsole.log("aa" < "ab");// Expected output: trueSyntax
x < yDescription
The operands are compared with multiple rounds of coercion, which can be summarized as follows:
- First, objects areconverted to primitives by calling its
[Symbol.toPrimitive]()(with"number"as hint),valueOf(), andtoString()methods, in that order. The left operand is always coerced before the right one. Note that although[Symbol.toPrimitive]()is called with the"number"hint (meaning there's a slight preference for the object to become a number), the return value is notconverted to a number, since strings are still specially handled. - If both values are strings, they are compared as strings, based on the values of the UTF-16 code units (not Unicode code points) they contain.
- Otherwise JavaScript attempts to convert non-numeric types to numeric values:
- Boolean values
trueandfalseare converted to 1 and 0 respectively. nullis converted to 0.undefinedis converted toNaN.- Strings are converted based on the values they contain, and are converted as
NaNif they do not contain numeric values.
- Boolean values
- If either value is
NaN, the operator returnsfalse. - Otherwise the values are compared as numeric values. BigInt and number values can be compared together.
Other operators, including>,>=, and<=, use the same algorithm as<. There are two cases where all four operators returnfalse:
- If one of the operands gets converted to a BigInt, while the other gets converted to a string that cannot be converted to a BigInt value (it throws asyntax error when passed to
BigInt()). - If one of the operands gets converted to
NaN. (For example, strings that cannot be converted to numbers, orundefined.)
For all other cases, the four operators have the following relationships:
x < y === !(x >= y);x <= y === !(x > y);x > y === y < x;x >= y === y <= x;Note:One observable difference between< and> is the order of coercion, especially if the coercion to primitive has side effects. All comparison operators coerce the left operand before the right operand.
Examples
>String to string comparison
"a" < "b"; // true"a" < "a"; // false"a" < "3"; // false"\uD855\uDE51" < "\uFF3A"; // trueString to number comparison
"5" < 3; // false"3" < 3; // false"3" < 5; // true"hello" < 5; // false5 < "hello"; // false"5" < 3n; // false"3" < 5n; // trueNumber to Number comparison
5 < 3; // false3 < 3; // false3 < 5; // trueNumber to BigInt comparison
5n < 3; // false3 < 5n; // trueComparing Boolean, null, undefined, NaN
true < false; // falsefalse < true; // true0 < true; // truetrue < 1; // falsenull < 0; // falsenull < 1; // trueundefined < 3; // false3 < undefined; // false3 < NaN; // falseNaN < 3; // falseComparison with side effects
Comparisons always coerce their operands to primitives. This means the same object may end up having different values within one comparison expression. For example, you may have two values that are both greater than and less than the other.
class Mystery { static #coercionCount = -1; valueOf() { Mystery.#coercionCount++; // The left operand is coerced first, so this will return 0 // Then it returns 1 for the right operand return Mystery.#coercionCount % 2; }}const l = new Mystery();const r = new Mystery();console.log(l < r && r < l);// trueWarning:This can be a source of confusion. If your objects provide custom primitive conversion logic, make sure it isidempotent: multiple coercions should return the same value.
Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-relational-operators> |