When should you use === vs ==, !== vs !=, etc.. in

2019-03-14 07:52发布

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?

What are the differences between === and ==, !== and ==... when should you use one and when should you use the other?

3条回答
贪生不怕死
2楼-- · 2019-03-14 08:27

=== and !== are the same as == and !=, but additionally do checks for the variable types.

查看更多
乱世女痞
3楼-- · 2019-03-14 08:28

=== is the Identity operator, and is used to test that value and type are equal.

so..

"3" == 3 // true
"3" === 3 // false
1 == true // true
1 === true // false
"1" == true // true
"1" === true // false

so when you care that value and type are equal, or not equal use Identity operators === or !==

查看更多
你好瞎i
4楼-- · 2019-03-14 08:37

The "normal" == operators in javascript perform type coercion, and try their best to do things like treat a string as number or an object as a string where required. The longer === operators will not do type coercion, but rather a strict comparison within the type.

查看更多
登录 后发表回答