严格平等 (===)
严格相等 (===) 运算符检查两个操作数是否相等,并返回布尔结果。与 equality 运算符不同,严格相等运算符始终认为不同类型的操作数是不同的。
¥The strict equality (===) operator checks whether its two operands are
equal, returning a Boolean result. Unlike the equality operator,
the strict equality operator always considers operands of different types to be
different.
Try it
语法
描述
¥Description
严格相等运算符(=== 和 !==)提供 IsStrictlyEqual 语义。
¥The strict equality operators (=== and !==) provide the IsStrictlyEqual semantic.
- 如果操作数类型不同,则返回
false。 - 如果两个操作数都是对象,则仅当它们引用同一对象时才返回
true。 - 如果两个操作数都是
null或两个操作数都是undefined,则返回true。 - 如果任一操作数为
NaN,则返回false。 - 否则,比较两个操作数的值:
- 数字必须具有相同的数值。
+0和-0被认为是相同的值。 - 字符串必须具有相同顺序的相同字符。
- 布尔值必须都是
true或都是false。
- 数字必须具有相同的数值。
该运算符与 equality (==) 运算符之间最显着的区别是,如果操作数属于不同类型,则 == 运算符会在比较之前尝试将它们转换为相同类型。
¥The most notable difference between this operator and the equality
(==) operator is that if the operands are of different types, the
== operator attempts to convert them to the same type before comparing.
示例
比较相同类型的操作数
比较不同类型的操作数
比较对象
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-equality-operators |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also