相等(==)
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.
Please take two minutes to fill out our short survey.
相等(==
)运算符检查其两个操作数是否相等,返回一个布尔值结果。与严格相等运算符不同,它会尝试转换不同类型的操作数,并进行比较。
尝试一下
console.log(1 == 1);
// Expected output: true
console.log("hello" == "hello");
// Expected output: true
console.log("1" == 1);
// Expected output: true
console.log(0 == false);
// Expected output: true
语法
x == y
描述
相等运算符(==
和 !=
)提供非严格相等语义。这可以大致总结如下:
- 如果操作数具有相同的类型,则按如下方式进行比较:
- 对象(Object):仅当两个操作数引用同一个对象时返回
true
。 - 字符串(String):仅当两个操作数具有相同的字符且顺序相同时返回
true
。 - 数字(Number):如果两个操作数的值相同,则返回
true
。+0
和-0
被视为相同的值。如果任何一个操作数是NaN
,返回false
;所以,NaN
永远不等于NaN
。 - 布尔值(Boolean):仅当操作数都为
true
或都为false
时返回true
。 - 大整型(BigInt):仅当两个操作数的值相同时返回
true
。 - 符号(Symbol):仅当两个操作数引用相同的符号时返回
true
。
- 对象(Object):仅当两个操作数引用同一个对象时返回
- 如果其中一个操作数为
null
或undefined
,另一个操作数也必须为null
或undefined
以返回true
。否则返回false
。 - 如果其中一个操作数是对象,另一个是原始值,则将对象转换为原始值。
- 在这一步,两个操作数都被转换为原始值(字符串、数字、布尔值、符号和大整型中的一个)。剩余的转换将分情况完成。
宽松相等是对称的:A == B
对于 A
和 B
的任何值总是具有与 B == A
相同的语义(应用转换的顺序除外)。
该运算符与严格相等(===
)运算符之间最显著的区别是,严格相等运算符不尝试类型转换。相反,严格相等运算符总是认为不同类型的操作数是不同的。严格相等运算符本质上只执行第 1 步,然后对所有其他情况返回 false
。
上面的算法有一个“故意违反”:如果其中一个操作数是 document.all
,则它被视为 undefined
。这意味着 document.all == null
是 true
,但 document.all === undefined && document.all === null
是 false
。
示例
没有类型转换的比较
1 == 1; // true
"hello" == "hello"; // true
有类型转换的比较
"1" == 1; // true
1 == "1"; // true
0 == false; // true
0 == null; // false
0 == undefined; // false
0 == !!null; // true,看看逻辑非运算符
0 == !!undefined; // true,看看逻辑非运算符
null == undefined; // true
const number1 = new Number(3);
const number2 = new Number(3);
number1 == 3; // true
number1 == number2; // false
比较对象
const object1 = {
key: "value",
};
const object2 = {
key: "value",
};
console.log(object1 == object2); // false
console.log(object1 == object1); // true
比较字符串和 String 对象
请注意,使用 new String()
构造的字符串是对象。如果将其中之一与字符串字面量进行比较,则该 String
对象将被转换为字符串字面量并对其内容进行比较。但是,如果两个操作数都是 String
对象,则将它们作为对象进行比较,并且必须引用相同的对象才会相等:
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
比较日期和字符串
const d = new Date("December 17, 1995 03:24:00");
const s = d.toString(); // 例如:“Sun Dec 17 1995 03:24:00 GMT+0800 (中国标准时间)”
console.log(d == s); // true
比较数组和字符串
const a = [1, 2, 3];
const b = "1,2,3";
a == b; // true,`a` 转换为字符串
const c = [true, 0.5, "hey"];
const d = c.toString(); // "true,0.5,hey"
c == d; // true
规范
Specification |
---|
ECMAScript® 2026 Language Specification # sec-equality-operators |