小于 (<)

如果左操作数小于右操作数,则小于 (<) 运算符返回 true,否则返回 false

¥The less than (<) operator returns true if the left operand is less than the right operand, and false otherwise.

Try it

语法

¥Syntax

js
x < y

描述

¥Description

操作数经过多轮强制转换对比,可以总结如下:

¥The operands are compared with multiple rounds of coercion, which can be summarized as follows:

  • 首先,对象通过按顺序调用其 [@@toPrimitive]()(以 "number" 作为提示)、valueOf()toString() 方法来获得 转换为基元。左操作数总是先于右操作数进行强制。请注意,虽然 [@@toPrimitive]() 是使用 "number" 提示调用的(这意味着对象稍微倾向于成为数字),但返回值不是 转换为数字,因为字符串仍然经过特殊处理。
  • 如果两个值都是字符串,则根据它们包含的 UTF-16 代码单元(而不是 Unicode 代码点)的值将它们作为字符串进行比较。
  • 否则 JavaScript 会尝试将非数字类型转换为数值:
    • 布尔值 truefalse 分别转换为 1 和 0。
    • null 转换为 0。
    • undefined 转换为 NaN
    • 字符串根据其包含的值进行转换,如果不包含数值则转换为 NaN
  • 如果任一值为 NaN,则运算符返回 false
  • 否则,这些值将作为数值进行比较。BigInt 和数值可以一起比较。

其他算子,包括 >>=<=,使用与 < 相同的算法。有两种情况所有四个运算符都返回 false

¥Other operators, including >, >=, and <=, use the same algorithm as <. There are two cases where all four operators return false:

  • 如果其中一个操作数转换为 BigInt,而另一个操作数转换为无法转换为 BigInt 值的字符串(传递给 BigInt() 时会抛出 语法错误)。
  • 如果其中一个操作数转换为 NaN。(例如,无法转换为数字的字符串,或 undefined。)

对于所有其他情况,四个运算符具有以下关系:

¥For all other cases, the four operators have the following relationships:

js
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

js
"a" < "b"; // true
"a" < "a"; // false
"a" < "3"; // false

"\uD855\uDE51" < "\uFF3A"; // true

字符串与数字的比较

¥String to number comparison

js
"5" < 3; // false
"3" < 3; // false
"3" < 5; // true

"hello" < 5; // false
5 < "hello"; // false

"5" < 3n; // false
"3" < 5n; // true

数字与数字的比较

¥Number to Number comparison

js
5 < 3; // false
3 < 3; // false
3 < 5; // true

数字与 BigInt 比较

¥Number to BigInt comparison

js
5n < 3; // false
3 < 5n; // true

比较布尔值、null、未定义、NaN

¥Comparing Boolean, null, undefined, NaN

js
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

与副作用的比较

¥Comparison 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.

js
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.

规范

Specification
ECMAScript Language Specification
# sec-relational-operators

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看