DEV Community

What is the Double bang (!!) operator in JavaScript?

Sanchithasr on December 31, 2020

Every value has truth or false values in JavaScript. For example, a null value has an associated boolean value of false. Similarly 34 has an associ...
Collapse
 
rvxlab profile image
RVxLab

This is very well explained. You can also call this the double NOT operator if you'd like.

While Boolean(someValue) the same things as !!someValue, using the double not operator is shorter to write and a tiny bit faster. The speed increase is nothing meaningful, but interesting nonetheless.

Benchmark I ran in case you want to see for yourself:

(function(){
    console.time('!!');

    for (let i = 0; i < 1000000; i++) {
        const number = 1;
        const bool = !!number;
    }

    console.timeEnd('!!');

    console.time('Boolean()');

    for (let i = 0; i < 1000000; i++) {
        const number = 1;
        const bool = Boolean(number);
    }

    console.timeEnd('Boolean()');
})();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sanchithasr profile image
Sanchithasr • Edited

This is interesting. Never thought about Boolean() . Thank you.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

It isn't a double bang operator, it is using the ! operator twice. Also, since - as you rightly state - values already have truthy and falsey-ness, it is largely pointless to use this unless you really, really need a pure boolean (perhaps for building some JSON to send somewhere on another system that requires the correct type)

Collapse
 
__mrvik__ profile image
MrViK

That rocks, I remember it as "Bang Bang, you're a boolean now".

Collapse
 
sanchithasr profile image
Sanchithasr

Bang Bang, you're a Boolean now

I totally had forgotten about this. :)

Collapse
 
wparad profile image
Warren Parad

I feel like they article made this whole concept really complicated. It's actually simple, What is !!?. It's the same as Coerce to boolean. It would do the same as a method Convert.toBoolean(value) where value is truthy or falsy and the result is true or false.

You need this because some parameters are expected to be true or false and not truthy and falsy. Boolean(value) doesn't actually coerce a value to a boolean, it's unfortunately less useful than that. And so using !! (the double NOT) is a great way to coerce a value to a boolean.

Collapse
 
sturpin profile image
Sergio Turpín

Cool!! I didn't know 👍

Collapse
 
sanchithasr profile image
Sanchithasr

👍

Collapse
 
amrishpandey profile image
amrishpandey • Edited

what i learned that if a value is true or false.
by default it does not return anything if it is not explicitly set with true or false boolean . that means by default it returns the value only.

if single bang (!) is added before the value, it returns false.
if double bang (!!) is added before the value, it returns true.

am i correct ?

Collapse
 
luriemartin profile image
david kane

@geometry dash lite Instead of using a double bang operator, it is twice using the! operator. As you properly point out, values already have truthiness and falseness, so using this is practically pointless unless you really, truly need a pure boolean (maybe for making some JSON to transmit someplace on another system that has the correct type)

Collapse
 
siamcr7 profile image
Jamil Siam

Good stuff!

Collapse
 
amircahyadi profile image
Amir-cahyadi

❤️ clear 👍👍

Collapse
 
ashlex21 profile image
ashlex21

Hey Sanchithasr! I'm stuck in a problem in React JS can you please help asap

Collapse
 
sanchithasr profile image
Sanchithasr

Hey. I have not worked on React much. Please post the problem. Someone might be able to help out. :)