JavaScript中的 ==、===、if()

本文详细解析了 JavaScript 中双等号 (==) 和三等号 (===) 的比较行为,展示了不同类型值间的比较结果,并强调了使用三等号的重要性以避免类型转换导致的意外结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自 :http://dorey.github.io/JavaScript-Equality-Table/


== (negated: !=)

When using two equals signs for JavaScript equality testing, some funky conversions take place.

 
true
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"true"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"false"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"0"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"-1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
undefined
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Infinity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-Infinity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[[]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NaN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Moral of the story:

Use three equals unless you fully understand the conversions that take place for two-equals.



===:

=== (negated: !==)

When using three equals signs for JavaScript equality testing, everything is as is. Nothing gets converted before being evaluated.

 
true
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"true"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"false"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"0"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
"-1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
undefined
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Infinity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-Infinity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[[]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
NaN
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Moral of the story:

Use three equals unless you fully understand the conversions that take place for two-equals.


if():

A standard IF statement. If(value) {/*- green -*/} else { /*- white -*/ }

Note: This row does not match up with any of the rows in the other table.

true
 
if (true) { /* executes */ }
false
 
if (false) { /* does not execute */ }
1
 
if (1) { /* executes */ }
0
 
if (0) { /* does not execute */ }
-1
 
if (-1) { /* executes */ }
"true"
 
if ("true") { /* executes */ }
"false"
 
if ("false") { /* executes */ }
"1"
 
if ("1") { /* executes */ }
"0"
 
if ("0") { /* executes */ }
"-1"
 
if ("-1") { /* executes */ }
""
 
if ("") { /* does not execute */ }
null
 
if (null) { /* does not execute */ }
undefined
 
if (undefined) { /* does not execute */ }
Infinity
 
if (Infinity) { /* executes */ }
-Infinity
 
if (-Infinity) { /* executes */ }
[]
 
if ([]) { /* executes */ }
{}
 
if ({}) { /* executes */ }
[[]]
 
if ([[]]) { /* executes */ }
[0]
 
if ([0]) { /* executes */ }
[1]
 
if ([1]) { /* executes */ }
NaN
 
if (NaN) { /* does not execute */ }
Moral of the story:

Use three equals unless you fully understand the conversions that take place for two-equals.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值