平等(==)

相等 (==) 运算符检查两个操作数是否相等,并返回布尔结果。与 严格平等 运算符不同,它尝试转换和比较不同类型的操作数。

¥The equality (==) operator checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, it attempts to convert and compare operands that are of different types.

Try it

语法

¥Syntax

js
x == y

描述

¥Description

相等运算符(==!=)提供 IsLooselyEqual 语义。这可以大致概括如下:

¥The equality operators (== and !=) provide the IsLooselyEqual semantic. This can be roughly summarized as follows:

  1. 如果操作数具有相同类型,则比较如下:
    • 对象仅当两个操作数引用同一对象时才返回 true
    • 字符串:仅当两个操作数具有相同顺序的相同字符时才返回 true
    • 数字:仅当两个操作数具有相同值时才返回 true+0-0 被视为相同的值。如果任一操作数为 NaN,则返回 false;因此,NaN 永远不等于 NaN
    • 布尔值:仅当操作数均为 true 或均为 false 时才返回 true
    • 大整数:仅当两个操作数具有相同值时才返回 true
    • 符合:仅当两个操作数引用相同符号时才返回 true
  2. 如果其中一个操作数是 nullundefined,则另一个操作数也必须是 nullundefined,才能返回 true。否则返回 false
  3. 如果其中一个操作数是对象,另一个是原语,则 将对象转换为原始对象
  4. 在此步骤中,两个操作数都转换为基元(String、Number、Boolean、Symbol 和 BigInt 之一)。其余的转换将根据具体情况进行。
    • 如果它们属于同一类型,请使用步骤 1 进行比较。
    • 如果其中一个操作数是符号而另一个不是,则返回 false
    • 如果其中一个操作数是布尔值,而另一个不是,则 将布尔值转换为数字true 转换为 1,false 转换为 0。然后再次松散地比较两个操作数。
    • 数字到字符串:将字符串转换为数字。转换失败会导致 NaN,这将保证相等为 false
    • 数字到 BigInt:通过它们的数值进行比较。如果数字为 ±Infinity 或 NaN,则返回 false
    • 字符串到 BigInt:使用与 BigInt() 构造函数相同的算法将字符串转换为 BigInt。如果转换失败,则返回 false

松散相等是对称的:对于 AB 的任何值,A == B 始终具有与 B == A 相同的语义(应用转换的顺序除外)。

¥Loose equality is symmetric: A == B always has identical semantics to B == A for any values of A and B (except for the order of applied conversions).

此运算符与 严格平等 (===) 运算符之间最显着的区别是严格相等运算符不尝试类型转换。相反,严格相等运算符始终认为不同类型的操作数是不同的。严格相等运算符本质上仅执行步骤 1,然后对于所有其他情况返回 false

¥The most notable difference between this operator and the strict equality (===) operator is that the strict equality operator does not attempt type conversion. Instead, the strict equality operator always considers operands of different types to be different. The strict equality operator essentially carries out only step 1, and then returns false for all other cases.

上述算法有一个 "故意违反":如果其中一个操作数是 document.all,则将其视为 undefined。这意味着 document.all == nulltrue,但 document.all === undefined && document.all === nullfalse

¥There's a "willful violation" of the above algorithm: if one of the operands is document.all, it is treated as if it's undefined. This means that document.all == null is true, but document.all === undefined && document.all === null is false.

示例

¥Examples

不进行类型转换的比较

¥Comparison with no type conversion

js
1 == 1; // true
"hello" == "hello"; // true

与类型转换的比较

¥Comparison with type conversion

js
"1" == 1; // true
1 == "1"; // true
0 == false; // true
0 == null; // false
0 == undefined; // false
0 == !!null; // true, look at Logical NOT operator
0 == !!undefined; // true, look at Logical NOT operator
null == undefined; // true

const number1 = new Number(3);
const number2 = new Number(3);
number1 == 3; // true
number1 == number2; // false

对象比较

¥Comparison of objects

js
const object1 = {
  key: "value",
};

const object2 = {
  key: "value",
};

console.log(object1 == object2); // false
console.log(object1 == object1); // true

比较字符串和 String 对象

¥Comparing strings and String objects

请注意,使用 new String() 构造的字符串是对象。如果将其中之一与字符串文字进行比较,则 String 对象将转换为字符串文字,并对内容进行比较。但是,如果两个操作数都是 String 对象,则它们将作为对象进行比较,并且必须引用同一对象才能成功进行比较:

¥Note that strings constructed using new String() are objects. If you compare one of these with a string literal, the String object will be converted to a string literal and the contents will be compared. However, if both operands are String objects, then they are compared as objects and must reference the same object for comparison to succeed:

js
const string1 = "hello";
const string2 = String("hello");
const string3 = new String("hello");
const string4 = new String("hello");

console.log(string1 == string2); // true
console.log(string1 == string3); // true
console.log(string2 == string3); // true
console.log(string3 == string4); // false
console.log(string4 == string4); // true

比较日期和字符串

¥Comparing Dates and strings

js
const d = new Date("1995-12-17T03:24:00");
const s = d.toString(); // for example: "Sun Dec 17 1995 03:24:00 GMT-0800 (Pacific Standard Time)"
console.log(d == s); //true

比较数组和字符串

¥Comparing arrays and strings

js
const a = [1, 2, 3];
const b = "1,2,3";
a == b; // true, `a` converts to string

const c = [true, 0.5, "hey"];
const d = c.toString(); // "true,0.5,hey"
c == d; // true

规范

Specification
ECMAScript Language Specification
# sec-equality-operators

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看