何时在 JavaScript 中使用双非 (!!) 运算符 [英] When to use the double not (!!) operator in JavaScript

查看:19
本文介绍了何时在 JavaScript 中使用双非 (!!) 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 双非运算符的作用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.

我说过 if (!!someVar) 从来没有意义,(!!someVar && ... 也没有意义,因为 if&& 将导致 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.

事实上,我唯一能想到使用 double not 运算符是合法的情况是,如果您想与另一个布尔值进行严格比较(因此可能在返回值中明确地期望 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 同时使用了 if (!!someVar)return !!someVar && 时,我开始怀疑自己....

Is this correct? I started to doubt myself when I noticed jQuery 1.3.2 used both if (!!someVar) and return !!someVar && ...

double 在这些情况下没有任何实际效果吗?

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.

推荐答案

if 语句 我和你在一起,它是完全安全的,因为在内部,ToBoolean 操作将在条件表达式上执行(参见步骤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";

总之,布尔逻辑运算符 可以返回一个操作数,而不一定是 Boolean 结果:

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

另一方面,逻辑或运算符 (||) 将返回 第二个操作数 的值,如果第一个是 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"

如果它本身是非假的,它将返回第一个操作数的值:

And it will return the value of the first operand if it is by itself non-falsy:

"foo" || "anything"; // "foo"

也许值得一提的是,falsy 值是:nullundefinedNaN0,零长度字符串,当然还有 false.

Maybe it's worth mentioning that the falsy values are: null, undefined, NaN, 0, zero-length string, and of course false.

布尔上下文,将返回 true.

这篇关于何时在 JavaScript 中使用双非 (!!) 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆