javascript - Not not (!!) 在 if 条件内

标签 javascript if-statement operators conditional

<分区>

注意:它实际上是 What is the difference between if(!!condition) and if(condition) 的副本

虽然我明白what the !! means (double not) ,出于同样的原因,它的使用对我来说没有意义 in the MDN documentation :

if (!!window.Worker) {
...
}

对于这种情况,这不是完全一样吗?

if (window.Worker) {
...
}

转换为 bool 值对我来说毫无意义,因为 if 只会在 window.Worker 存在时执行。对于 if() 条件(我认为)来说,说它是 TrueObject 是一样的。

那么,为什么要在这里使用 !! 呢?或者,为什么 window.Workerif() 中转换为 bool 值?

最佳答案

是的,完全一样。没有什么要补充的。

它可能被用来强调 window.Worker 属性 - 应该是一个函数 - 被转换为 bool 值以检测它的存在,而不是看起来像一个被遗忘的 ( ) 调用。不管怎样,it is now gone .

关于javascript - Not not (!!) 在 if 条件内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30296222/

相关文章:

javascript - 带有工具提示的 id base anchor 滚动链接

javascript - jQuery Star Rating Plugin 如何在点击事件中从禁用切换到启用

php - 下拉菜单和 where 语句

java - If 语句不能与 OR 运算符一起使用

java - 在 Android 中按下按钮时如何设置单选按钮的文本颜色?

javascript - JS 双感叹号——有什么好的理由使用它吗?

datetime - 对 Date 对象使用 Groovy 比较运算符

javascript - 数组没有预期值 - Nodejs

javascript - 如何将热图包裹在螺旋(季节性螺旋)- d3 上?

ruby - Ruby 中是否有 "&& Equals"运算符