DEV Community

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

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.