何时在 JavaScript 中使用双非 (!!) 运算符

2023-09-04前端开发问题
9

本文介绍了何时在 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 中使用双非 (!!) 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13