Not not (!!) 在 if 条件内

Not not (!!) inside if condition

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

的副本

虽然我明白了what the !! means (double not), for this same reason it doesn't make sense to me its use in the MDN documentation:

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

这不是和这个情况完全一样吗?

if (window.Worker) {
...
}

转换为布尔值对我来说毫无意义,因为 if 只有在 window.Worker 存在时才会执行。说 TrueObject 对于 if() 条件(我认为)是相同的。

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

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

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