The <
operator returns true
if the left operand is less than the right operand, and false
otherwise.
The <
operator returns true
if the left operand is less than the right operand, and false
otherwise.
x < y
The operands are compared with multiple rounds of coercion, which can be summarized as follows:
[@@toPrimitive]()
(with "number"
as hint), valueOf()
, and toString()
methods, in that order. The left operand is always coerced before the right one. Note that although [@@toPrimitive]()
is called with the "number"
hint (meaning there's a slight preference for the object to become a number), the return value is not converted to a number, since strings are still specially handled.true
and false
are converted to 1 and 0 respectively.null
is converted to 0.undefined
is converted to NaN
.NaN
if they do not contain numeric values.NaN
, the operator returns false
.Other operators, including >
, >=
, and <=
, use the same algorithm as <
. There are two cases where all four operators return false
:
BigInt()
).NaN
. (For example, strings that cannot be converted to numbers, or undefined
.)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.
"a" < "b"; // true "a" < "a"; // false "a" < "3"; // false "\uD855\uDE51" < "\uFF3A"; // true
"5" < 3; // false "3" < 3; // false "3" < 5; // true "hello" < 5; // false 5 < "hello"; // false "5" < 3n; // false "3" < 5n; // true
5 < 3; // false 3 < 3; // false 3 < 5; // true
5n < 3; // false 3 < 5n; // true
true < false; // false false < true; // true 0 < true; // true true < 1; // false null < 0; // false null < 1; // true undefined < 3; // false 3 < undefined; // false 3 < NaN; // false NaN < 3; // false
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); // true
Warning: This can be a source of confusion. If your objects provide custom primitive conversion logic, make sure it is idempotent: multiple coercions should return the same value.
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
Less_than |
1 | 12 | 1 | 3 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1.0 | 0.10.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than