Article originally published on Will’s personal blog
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)
.
Top comments (5)
It’s commonly called ‘bang bang’
Bang bang! You're Boolean now!
Which sounds eerily similar to "gang bang" ;)
Yes this is a well-known JS idiom, you see it a lot in JS source code. Goes to show what a quirky language JS really is (what you see is that newer versions of JS like ES2015 are trying to clean up & rationalize the language, at least that's my interpretation of it).
The bitwise NOT operator can be used as a poor-mans integer cast. I don't know what they call it but I'm really hoping it's "squiggiggle"
~~value
effectively replacesMath.trunc(Number(value) || 0)
I have to add that it's clearly a confusing hack. They're both hacks. If you want a boolean from a value, please use
Boolean(value)
. Do it for your readers.Squiggiggle essentially casts its input to a 32-bit signed integer (truncating decimals toward 0), or 0 if there was no reasonable integer to choose...
Decimals...
Non-numbers work as you might expect...
But be careful if you're passing in large numbers, as they will be clipped to a 32 bit signed integer.
-~number
is a fun one to play with too, a bit useless but a good exercise in 2's complement for any teachers out there.Some comments may only be visible to logged-in visitors. Sign in to view all comments.