101

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?

5
  • 6
    Based on the answers given here 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, 2014 at 6:15
  • I'd say it probably just got "simplified" into that expression by manually removing parens.
    – cwharris
    Jan 16, 2014 at 6:27
  • 3
    Perhaps someone thought that three exclamation marks would be more visible than one? Jan 16, 2014 at 6:29
  • 18
    Nope. !a is already a boolean, so !a, !!!a, !!!!!a, etc. are all identical.
    – Blender
    Jan 16, 2014 at 6: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, 2021 at 18:42

4 Answers 4

139

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.

6
  • 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! Sep 8, 2014 at 19:22
  • And if a is null ? and you want only false value. !a return true and !!!a false ?
    – Dujard
    May 19, 2021 at 13:40
  • @Dujard !!!null is true. One exclamation will always be the same as three.
    – ICW
    Jun 29, 2021 at 19:22
  • @Dujard !!!null is true. One exclamation will always be the same as three.
    – ICW
    Jun 29, 2021 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 1, 2021 at 8:56
21

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.

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.

3
  • 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. May 28, 2015 at 20:46
  • This answer works perfectly for JavaScript, particularly when used in w3schools.com/xml/xml_cdata.asp Aug 28, 2015 at 9:08
  • Do you mean the interpreter? JS does not have a compiler right? Maybe on nodejs where they use GoogleV8 engine only? Nov 26, 2019 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.

5
  • 2
    That's all fine, but it doesn't answer the question.
    – blm
    Nov 6, 2015 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, 2015 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, 2015 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, 2015 at 15:38
  • 2
    Oh, wait...yeah; I get it now. Sorry for the confusion.
    – theGleep
    Nov 12, 2015 at 15:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.