!!, which is used to create a Boolean from a value.
For example, consider the code below, which returns True because there is a value (a string) for the variable
var foo = 'hello'; var baz = !!foo; baz; // true
But there is no
!! operator, just the logical NOT or negate operator,
!, used twice.
The negate operator
! works in a two-step process:
- if the value passed is not already a Boolean, coerce it into a Boolean
- flip the pairing to negate it
By adding a second negate operator we merely flip the sign back since our value is already a Boolean thanks to the first
There is a better way.
Use the much more explicit Boolean object.
var foo = 'hello'; var baz = Boolean(foo); baz; // true
So much better! Problem solved.