简体   繁体   English

为何使用!! 将变量强制转换为布尔值以用于条件表达式?

[英]Why use !! to coerce a variable to boolean for use in a conditional expression?

!!x coerces the type of variable x to a boolean, whilst maintaining its truthiness or lack thereof - see this question - I have a question about the use of this in conditional expressions. !!x将变量x的类型强制转换为布尔值,同时保持其真实性或缺乏真实性 - 请参阅此问题 - 我对在条件表达式中使用它有疑问。

A few times in JS code I've seen !! 在JS代码中我见过几次!! used to coerce a variable to boolean type in an if condition like so 用于在if条件下将变量强制转换为boolean类型

if(!!x) {
    x.doStuff();
}

Where the idea is to test if x is defined before calling methods on it. 其中的想法是在调用方法之前测试是否定义了x

But, in my own code I've always just used 但是,在我自己的代码中,我总是使用它

if(x) {
    x.doStuff();
}

On the premise that if x is defined, the condition will pass, and if x is undefined, it will not pass. 在如果定义了x的前提下,条件将通过,如果x未定义,则不会通过。

So my question is what is the point of coercing x to a boolean using !! 所以我的问题是使用!! x强制转换为布尔值什么意思!! in this scenario? 在这种情况下? What does this code do that this code doesn't? 什么是这个代码这个代码不?

In that specific context, I would say that there is no difference between explicitely converting to boolean using !! 在那个特定的上下文中,我会说使用!!明确转换为布尔值之间没有区别!! or let the if expression being converted to a boolean naturally. 或者让if表达式自然地转换为布尔值。 What I mean by this is that if (x) will be interpreted as if (Boolean(x)) , which is the same as if (!!x) . 我的意思是if (x)将被解释为if (Boolean(x)) ,它与if (!!x)

However, if you are returning a value from a function, for instance if you want to implement a arrayHasItems function, you could implement it this way: 但是,如果要从函数返回值,例如,如果要实现arrayHasItems函数,则可以通过以下方式实现它:

function arrayHasItems(arr) {
    return arr.length;
}

Using the function in a if statement as is would work because the numerical value returned from the function would be converted to a boolean value. 在if语句中使用函数将起作用,因为从函数返回的数值将转换为布尔值。 However, the client code expects the function to return a boolean value, so he might be checking the condition by doing: 但是,客户端代码期望函数返回一个布尔值,因此他可能通过执行以下操作来检查条件:

if (arrayHasItems(arr) === true) {}

In this case it would fail, because the returned result from arrayHasItems was a number. 在这种情况下,它会失败,因为来自arrayHasItems的返回结果是一个数字。 Therefore, it would have been better to implement the function by returning a boolean like expected. 因此,通过返回类似于expect的布尔值来实现该函数会更好。

function arrayHasItems(arr) {
    return !!arr.length;
}

EDIT: 编辑:

This brings up a new question: why !!arr.length and not just arr.length > 0 这提出了一个新问题:为什么!! arr.length而不仅仅是arr.length> 0

There isin't any difference between both in the result produced and you are not even saving bytes since both statements take the same amount of characters. 在产生的结果和你甚至没有保存字节之间没有任何区别,因为两个语句都使用相同数量的字符。 However I created a test case and the double negation seems to perform better, but it might not be consistent across all browsers. 但是我创建了一个测试用例 ,双重否定似乎表现得更好,但它可能在所有浏览器中都不一致。

As stated it converts a value to a boolean value and is therefore not much different from if(x) . 如上所述,它将值转换为布尔值,因此与if(x)没有太大区别。 However, both are tricky as it will also convert 0 to false and other such things. 但是,两者都很棘手,因为它也会将0转换为false和其他类似的东西。

It has to do with JavaScript type coersion. 它与JavaScript类型的coersion有关。

Often you want to check if an object is not null, not undefined, not 0, etc. The common way to check for that is 通常你想检查对象是否为空,未定义,不是0等。检查对象的常用方法是

if (obj) {
  ...
}

However, if you want the condition to be equal to true or false the ! 但是,如果你想要条件等于truefalse ! can be used on non-boolean object instances. 可以在非布尔对象实例上使用。 Here is an example that uses an equality operator === that does not do type coersion. 下面是一个使用等值运算符===的示例,该运算符不执行类型coersion。

var obj = {};

console.log(obj === true); // returns false
console.log(!obj === false); // returns true
console.log(!!obj === true); // returns true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Eslint:在条件表达式中不必要使用布尔文字 - Eslint: Unnecessary use of boolean literals in conditional expression 在条件表达式中不必要地使用 Boolean 文字 - Unnecessary use of Boolean literals in conditional expression 错误:在条件表达式中不必要地使用布尔文字 - Error: Unnecessary use of boolean literals in conditional expression Javascript ESLint错误-条件表达式中不必要使用布尔文字 - Javascript ESLint Error - Unnecessary use of boolean literals in conditional expression 错误在条件表达式中不必要地使用布尔文字 no-unneeded-ternary - error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary JavaScript变量声明/初始化可以使用条件类型的布尔值吗? - Can a JavaScript variable declaration/initialization use a boolean type conditional? Javascript:错误在条件表达式 no-unneeded-ternary 中不必要地使用 boolean 文字 - Javascript: error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary 有条件的情况下可变使用不起作用? - Variable use in conditional not working? 不使用关系运算符的 JavaScript 条件表达式 - JavaScript Conditional expression without the use of Relational operator 无条件使用条件表达式进行默认赋值 - Unconditional use of conditional expression for default assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM