JavaScript: Double Not Operator?

Here's a little JavaScript tidbit I learned about recently.

The ! operator is a familiar face in the JavaScript landscape, showing up when we need to negate a value.

let thing = true
thing = !thing
thing === false // true

Here we take a Boolean value (true) and invert it to the opposite value (false).

This also works with non-Boolean values as well, since all values in JavaScript are either truthy or falsy.

let thing = "This string is truthy!"
thing = !thing
thing === false // true

Here we can see how the ! operator casts the value from a string value (truthy) to it's opposite Boolean value false. The same conversion happens when we use the ! operator on any value in JavaScript. If the value is already truthy it is cast to false, and vice versa.

It follows then that !! just performs this inversion twice.

let thing = "This string is truthy!"
thing = !!thing
thing === true // true

This is really just a shorter way to cast a value to it's Boolean counterpart. If a value is already falsy, using !! will result in the value being cast to false.

We could just as well do this for the same result:

let thing = Boolean("This string is truthy!")
thing === true // true

What's the difference? As far as I can tell, there is none!

Although !! is more succinct, the argument could be made that it is more confusing syntactically than Boolean(value).

Now What?

If you enjoyed this post, I'd love to continue the conversation on Twitter.

Social media not your thing? Shoot me an email: will@willharris.dev

Designed and developed by Will Harris

Built with

Hosted on

Social

Twitter

GitHub

LinkedIn