【发布时间】:2012-03-06 06:43:14
【问题描述】:
可能重复:
What is the !! (not not) operator in JavaScript?
What does the !! operator (double exclamation point) mean in JavaScript?
所以我在调试一些代码时遇到了这个问题:
var foo.bar = 0; // this is actually passed from another function, adding it for context
function(foo) {
var someVar = !!foo.bar;
if (foo.bar) {
// ..stuff happens
} else {
// .. something else happens
}
}
好的,我的问题是!! 的意义何在?所做的只是制作0 === false。
与
boolean(foo.bar)相比,使用它有什么好处吗?foo.bar 可以在 if 中进行评估,因为
0 === false已经存在,那么为什么要进行转换呢? (someVar 不会在其他任何地方重用)
【问题讨论】:
-
好吧,我知道它的作用,我只是想知道您的链接问题中没有解释的好处是什么。
-
0==falsenot0===false三等号可防止隐式转换尝试。
标签: javascript