When should you use === vs ==, !== vs !=, etc.. in javascript? [duplicate]

和自甴很熟 提交于 2019-12-20 11:04:06

问题


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

What are the differences between === vs == and !== vs !=?

When should you use each one?


回答1:


=== 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 !==




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/1094531/when-should-you-use-vs-vs-etc-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!