Javascript if语句双感叹号运算符冗余 [英] Javascript if statement double exclamation mark operator redundancy
问题描述
如果我的if语句没有条件运算符,那么使用双重感叹号运算符是否重要 - !!
?
If I have an if statement that doesn't have a conditional operator, does it matter if I use the double exclamation mark operator - !!
?
例如......
if ([]) {
}
vs ...
if (!!([])) {
}
根据我的理解 - 操作员基本上都在问 - 这个价值是真的吗?。所以在这种情况下它是多余的。在所有类似的情况下都是多余的吗?
From what I understand - the operator is basically asking - "is this value truthy?". So it is redundant in this case. Is it redundant in all cases similar to this?
编辑:
if (x) { console.log("hi"); }
if (!!x) { console.log("hi"); }
这两个打印是否适用于任何 x
?这是我的问题。
Will both of these print for any x
? That is my question.
推荐答案
是的。这两个都将打印任何布尔值(x)=== true
Yes. Both of these will print for any Boolean(x) === true
有关详细信息,您可以找到 truthy , falsy 和布尔值
For more detail, you can find truthy, falsy and Boolean in JavaScript
为了便于理解,您可以使用 Boolean([])// true
For esier to understand, you can use Boolean([]) // true
这篇关于Javascript if语句双感叹号运算符冗余的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!