Looking through the source code of one of our projects, I've found some amount of places where we're using three exclamation marks in conditional statements, like so:
通过查看我们其中一个项目的源代码,我发现了一些我们在条件语句中使用三个感叹号的地方,如下所示:
if (!!!someVar) {
// ...
}
Now, I understand that this isn't some kind of rarely used operator, it's just three negations in a row, like !(!(!someVar)))
. I don't understand what's the use of it - in my opinion it can safely be replaced with single exclamation mark. Following are my attempts to find a case when !!!a
isn't equal to !a
(taken straight from the google chrome console):
现在,我明白这不是某种很少使用的运算符,它只是连续三个否定,比如!(!(!someVar)))。我不明白它的用途是什么 - 在我看来它可以安全地用单个感叹号代替。以下是我试图找到一个案例,当!!! a不等于!a(直接从谷歌Chrome控制台获取):
var a = ''
""
!!!a === !a
true
a = 'string'
"string"
!!!a === !a
true
a = null
null
!!!a === !a
true
a = 12
12
!!!a === !a
true
a = {b: 1}
Object {b: 1}
!!!a.c === !a.c // a.c is undefined here
true
a = []
[]
!!!a === !a
true
a = [1,2]
[1, 2]
!!!a === !a
true
Am I missing some rare (or obvious) case?
我错过了一些罕见(或明显)的案例吗?
5 个解决方案
#1
53
There is no difference between !a
and !!!a
, since !!!a
is just !!(!a)
and because !a
is a boolean, !!(!a)
is just its double negation, therefore the same.
!a和!!! a之间没有区别,因为!!! a只是!!(!a)因为!a是布尔值,!!(!a)只是它的双重否定,因此是相同的。
#2
8
It is the same as one exclamation mark. The key idea behind it is to improve visibility for the programmer. Compiler will optimize it as single '!' anyway.
它与一个感叹号相同。其背后的关键思想是提高程序员的可见性。编译器会将其优化为单个'!'无论如何。
#3
4
Consider:
考虑:
var x;
console.log(x == false);
console.log(!x, !x == true);
console.log(!!x, !!x == true, !!x == false);
... the console output is:
...控制台输出是:
false
true true
false false true
notice how, even though x is "falsey" in the first use, it is not the same as false.
请注意,即使x在第一次使用中是“假的”,它也与false不同。
But the second use (!x) has an actual boolean - but it's the opposite value.
但第二次使用(!x)有一个实际的布尔值 - 但它是相反的值。
So the third use (!!x) turns the "falsey" value into a true boolean.
所以第三次使用(!! x)将“falsey”值转换为真正的布尔值。
...with that in mind, the third exclamation point makes a TRUE negation of the original value (a negation of the "true boolean" value).
...考虑到这一点,第三个感叹号对原始值进行TRUE否定(否定“真正的布尔值”)。
ETA:
ETA:
OMG! I can't believe I didn't notice that this is a TRIPLE exclamation point question! Even after it was specifically pointed out to me.
我的天啊!我无法相信我没有注意到这是一个三重惊叹号的问题!甚至在特别指出我之后。
So, while my answer is hopefully useful to someone, I have to agree with the others who have posted that a triple-exclamation is functionally the same as a single.
因此,虽然我的答案对某人有用,但我必须同意其他人发布的消息,即三重感叹号在功能上与单个感叹号相同。
#4
-2
The first double exclamation marks turned to a boolean value any falsy value of the object (undefined, null, 0) or any truthy value, then the third one negates it.
第一个双感叹号变为布尔值,对象的任何虚假值(undefined,null,0)或任何真值,然后第三个否定它。
#5
-2
Also, it's like exit
when you use it in your console.
此外,当您在控制台中使用它时,它就像退出一样。
#1
53
There is no difference between !a
and !!!a
, since !!!a
is just !!(!a)
and because !a
is a boolean, !!(!a)
is just its double negation, therefore the same.
!a和!!! a之间没有区别,因为!!! a只是!!(!a)因为!a是布尔值,!!(!a)只是它的双重否定,因此是相同的。
#2
8
It is the same as one exclamation mark. The key idea behind it is to improve visibility for the programmer. Compiler will optimize it as single '!' anyway.
它与一个感叹号相同。其背后的关键思想是提高程序员的可见性。编译器会将其优化为单个'!'无论如何。
#3
4
Consider:
考虑:
var x;
console.log(x == false);
console.log(!x, !x == true);
console.log(!!x, !!x == true, !!x == false);
... the console output is:
...控制台输出是:
false
true true
false false true
notice how, even though x is "falsey" in the first use, it is not the same as false.
请注意,即使x在第一次使用中是“假的”,它也与false不同。
But the second use (!x) has an actual boolean - but it's the opposite value.
但第二次使用(!x)有一个实际的布尔值 - 但它是相反的值。
So the third use (!!x) turns the "falsey" value into a true boolean.
所以第三次使用(!! x)将“falsey”值转换为真正的布尔值。
...with that in mind, the third exclamation point makes a TRUE negation of the original value (a negation of the "true boolean" value).
...考虑到这一点,第三个感叹号对原始值进行TRUE否定(否定“真正的布尔值”)。
ETA:
ETA:
OMG! I can't believe I didn't notice that this is a TRIPLE exclamation point question! Even after it was specifically pointed out to me.
我的天啊!我无法相信我没有注意到这是一个三重惊叹号的问题!甚至在特别指出我之后。
So, while my answer is hopefully useful to someone, I have to agree with the others who have posted that a triple-exclamation is functionally the same as a single.
因此,虽然我的答案对某人有用,但我必须同意其他人发布的消息,即三重感叹号在功能上与单个感叹号相同。
#4
-2
The first double exclamation marks turned to a boolean value any falsy value of the object (undefined, null, 0) or any truthy value, then the third one negates it.
第一个双感叹号变为布尔值,对象的任何虚假值(undefined,null,0)或任何真值,然后第三个否定它。
#5
-2
Also, it's like exit
when you use it in your console.
此外,当您在控制台中使用它时,它就像退出一样。