let thing = true thing = !thing thing === false // true
Here we take a Boolean value (
true) and invert it to the opposite value (
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
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
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!
!! is more succinct, the argument could be made that it is more confusing syntactically than