何时在 JavaScript 中使用 === 运算符检查?
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
As the title states; when should you use the ===
operator check when using JavaScript, and when not to.
Edit: more complete answer found here. Thanks to Mark Byers for pointing it out.
_L
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是严格类型相等运算符。它不仅检查两个值是否相等,而且类型相同。
考虑比较数字或字符串时的情况:
but
and
这适用于对象和数组。
因此,在上述情况下,您必须做出明智的选择,是使用
==
还是===
使用
===
是个好主意当你也确定类型时It is strict type equality operator. It not only checks whether two are equal in value but also of the same type.
Consider a situation when you compare numbers or strings:
but
and
This applies to objects as well as arrays.
So in above cases, you have to make sensible choice whether to use
==
or===
It is good idea to use
===
when you are sure about the type as well当您希望禁止隐式类型转换时。例如:
是
true
,而这不是:Douglas Crockford 建议始终使用严格比较。
When you wish to inhibit implied typecasts. For example:
is
true
, whereas this is not:Douglas Crockford recommends always using strict comparison.
您可以使用它来检查变量的包含值和类型是否与比较变量相同。
You use it to check if a variable's containing value and type is same as the compared one.