100

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):

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?

aga
  • 27,954
  • 13
  • 86
  • 121
  • 5
    Based on the answers given [here](http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript) it seems that `!!` is a shorthand way to "cast to Boolean", and thus `!!!` casts to the negated Boolean. Like you, I can't think of a time when a single `!` would not do just as well... – Floris Jan 16 '14 at 06:15
  • I'd say it probably just got "simplified" into that expression by manually removing parens. – cwharris Jan 16 '14 at 06:27
  • 3
    Perhaps someone thought that three exclamation marks would be more visible than one? – HandyAndyShortStack Jan 16 '14 at 06:29
  • 18
    Nope. `!a` is already a boolean, so `!a`, `!!!a`, `!!!!!a`, etc. are all identical. – Blender Jan 16 '14 at 06:52
  • 2
    People will say it's for readability but it's really not more readable. Really isn't an argument that makes any sense as to why you would use 3 exclamation points. If 3 exclamation points is more readable than 1, then 5 should be more readable than 3? Why not just use 10001 exclamation points for maximum readability? – ICW Sep 30 '21 at 18:42

4 Answers4

135

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.

Sophiα2329
  • 1,601
  • 1
  • 13
  • 15
  • 7
    I decided to delete my post in this thread since it was misleading. I'd like to thank **aga** and **@jason-goemaat** for rectifying my mistakes in their comments. I'm commenting here, becouse above is the most straightforward and correct answer. Cheers! – Konrad Talik Sep 08 '14 at 19:22
  • And if a is null ? and you want only false value. !a return true and !!!a false ? – Dujard May 19 '21 at 13:40
  • @Dujard `!!!null` is true. One exclamation will always be the same as three. – ICW Jun 29 '21 at 19:22
  • @Dujard `!!!null` is true. One exclamation will always be the same as three. – ICW Jun 29 '21 at 19:23
  • 2
    in short the OP's colleagues never gave any thought to what they were writing. using 3x is useless. – Hop hop Jul 01 '21 at 08:56
  • Technically, there is no difference. But, as [@zoidbergseasharp is pointing out in their answer](https://stackoverflow.com/a/57828405/2445204), it can be used as an indication to the human, that the original value was not a boolean itself but a truthy/falsy value. – Marcel Waldvogel Aug 31 '22 at 11:20
20

In JavaScript, it's a way to show that what you are evaluating is not a boolean but a truthy or falsy value.

So for truthy/falsy values you either use !! or !!!.

And for boolean values you either use ! or nothing.

This is simply for readability.

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Zoidbergseasharp
  • 3,697
  • 1
  • 11
  • 20
16

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.

Filip
  • 2,244
  • 2
  • 21
  • 34
  • Interesting interpretation of the motive. In C++ I find that the readability issue is best addressed with **not** *(which has been part of the language standard since the beginning but people don't exercise it... you can't call a variable that, so why not use it?)* Then again, I also use **and** / **or** instead of **&&** / **||** in "definitely C++ and not C code". So I'm weird. – HostileFork says dont trust SE May 28 '15 at 20:46
  • This answer works perfectly for JavaScript, particularly when used in http://www.w3schools.com/xml/xml_cdata.asp – nicolallias Aug 28 '15 at 09:08
  • Do you mean the interpreter? JS does not have a compiler right? Maybe on nodejs where they use GoogleV8 engine only? – Nick Prozee Nov 26 '19 at 12:08
6

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.

But the second use (!x) has an actual boolean - but it's the opposite value.

So the third use (!!x) turns the "falsey" value into a true boolean.

...with that in mind, the third exclamation point makes a TRUE negation of the original value (a negation of the "true boolean" value).

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.

theGleep
  • 1,179
  • 8
  • 14
  • 2
    That's all fine, but it doesn't answer the question. – blm Nov 06 '15 at 21:18
  • I should have said a simple "yes" or "no"? The answer to the question "am I missing something" is implied with the first line; "x is' falsey' but it's not the same as false". – theGleep Nov 10 '15 at 19:54
  • The question is about the triple exclamation point, which your answer doesn't mention at all. While the answer can be determined from your answer by someone sufficiently knowledgeable with JavaScript, if the poster were that knowledgeable, they wouldn't have had to ask the question in the first place. – blm Nov 10 '15 at 20:17
  • Hmm...I suppose I *should* have included the console output. I've added that to help make things more clear. I'm afraid I don't see how I'm not addressing the triple-exclamation point. – theGleep Nov 12 '15 at 15:38
  • 2
    Oh, wait...yeah; I get it now. Sorry for the confusion. – theGleep Nov 12 '15 at 15:39