当在JavaScript中使用双不(!)运算符 [英] When to use the double not (!!) operator in JavaScript
问题描述
我理解<一个href=\"http://stackoverflow.com/questions/1406604/what-does-the-operator-double-exclamation-point-mean-in-javascript\">what双运营商没有做在JavaScript中。我很好奇它虽然使用以及是否最近的说法,我做是正确的。
I understand what the double not operator does in JavaScript. I'm curious about it's use though and whether or not a recent assertion that I made is correct.
我说,如果(!someVar)
是从来没有意义也不是(!! someVar&放大器;&安培; ...
,因为无论是如果
和&放大器;&安培;
将导致someVar被评价为一个布尔值等等对!是多余的。
I said that if (!!someVar)
is never meaningful nor is (!!someVar && ...
because both the if
and the &&
will cause someVar to be evaluated as a boolean so the !! is superfluous.
在事实上,我能想到的,这将是合法的使用双没有运营商唯一的一次是,如果你想要做一个严格的比较,另一个布尔值(所以也许在预计true或false明确的返回值)。
In fact, the only time that I could think of that it would be legitimate to use the double not operator is if you wanted to do a strict comparison to another boolean value (so maybe in return value that expects true or false explicitly).
这是正确的?我开始怀疑自己时,我注意到的jQuery 1.3.2同时使用如果(!someVar)
和回报!someVar&放大器;&安培; ...
Is this correct? I started to doubt myself when I noticed jQuery 1.3.2 used both if (!!someVar)
and return !!someVar && ...
请问双没有在这种情况下任何实际的影响?
Does the double not have any actual effect in these situations?
我个人的看法是,它只是导致混乱。如果我看到一个if语句,我知道这是评估它作为一个布尔值。
My personal opinion is that it just leads to confusion. If I see an if statement, I know it's evaluating it as a boolean.
推荐答案
在 如果
语句的我和你在一起,这是完全安全的,因为内部的 ToBoolean
的操作将在条件执行前pression(请参阅步骤3规格的)。
In the context of if
statements I'm with you, it is completely safe because internally, the ToBoolean
operation will be executed on the condition expression (see Step 3 on the spec).
但是,如果你想,可以说,从一个函数返回一个布尔值,你应该确保其结果将是实际布尔值,例如:
But if you want to, lets say, return a boolean value from a function, you should ensure that the result will be actually boolean, for example:
function isFoo () {
return 0 && true;
}
console.log(isFoo()); // will show zero
typeof isFoo() == "number";
总之, 逻辑运算符可以返回一个操作数 ,而不是一个布尔
的结果必然:
In conclusion, the Boolean Logical Operators can return an operand, and not a Boolean
result necessarily:
逻辑运算符(&放大器;&安培;
),将返回的第二个操作数的值的,如果第一个是的真正的:
The Logical AND operator (&&
), will return the value of the second operand if the first is truly:
true && "foo"; // "foo"
和它将返回的第一个操作数的价值,如果它是由自身的 falsy 的:
And it will return the value of the first operand if it is by itself falsy:
NaN && "anything"; // NaN
0 && "anything"; // 0
在另一方面,逻辑OR运算符( ||
)将返回的值的第二个操作数的,如果第一个是的 falsy 的:
On the other hand, the Logical OR operator (||
) will return the value of the second operand, if the first one is falsy:
false || "bar"; // "bar"
和它将返回的第一个操作数的价值,如果它本身就是非falsy:
And it will return the value of the first operand if it is by itself non-falsy:
"foo" || "anything"; // "foo"
也许这是值得一提的是, falsy 的值是:空
,未定义
, NaN的
, 0
,零长度字符串,当然假
。
Maybe it's worth mentioning that the falsy values are: null
, undefined
, NaN
, 0
, zero-length string, and of course false
.
还有什么(是不是的 falsy 的一个布尔
对象或布尔
值),在的布尔上下文的,将返回<$ C $评估C>真正。
Anything else (that is not falsy, a Boolean
object or a Boolean
value), evaluated in boolean context, will return true
.
这篇关于当在JavaScript中使用双不(!)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!