Bang Bang

Ryan Ponte
3 min readSep 9, 2015

I really like the double bang “operator” (!!) in Ruby, but I’ve struggled to find a use for it. For those who don’t know, a double bang, represented by two exclamation points, is a way of converting (or coercing) truthy / falsey values into strict Boolean data types. The double bang isn’t really an operator itself, but is made up of two not-operators, or bangs. The not-operator (!) takes true values and makes them false or false values and makes them true.

BANG!!

When using the double not-operator, the first bang makes the data it’s operating on into a Boolean before negating it. A truthy value would become the Boolean ‘false’ and a falsey value would become the Boolean ‘true.’ The second bang then flips the resultant Boolean back to the appropriate value. Thus, a double bang would make a truthy value into the Boolean ‘true,’ and the same for falsey to ‘false.’ In Ruby, only false and nil are falsey, so the double bang isn’t all that useful. You might use it in a method that you want to return a Boolean value. For example:

def accepted?
!!flatiron_email
end

In the example above, the ‘accepted?’ method will return true unless the value of flatiron_email is false or nil.

JavaScript also has the double bang, and it has more ways for a value to be falsey. In JavaScript, the following values are equivalent to false in conditional statements:

* false
* null
* undefined
* The empty string "" or ''
* The number 0
* The number NaN (Not a Number)

All other values are truthy. Some examples:

!!false === false
!!true === true

!!0 === false
!!parseFloat("bang") === false // NaN is falsey
!!1 === true
!!-1 === true // -1 is truthy

!!"" === false // empty string is falsey
!!"bang" === true // non-empty string is truthy
!!"false" === true // ...even if it contains a falsey value

!!window.bang === false // undefined is falsey
!!null === false // null is falsey

!!{} === true // an (empty) object is truthy
!![] === true // an (empty) array is truthy

You can also use the expression Boolean(argument) to convert an argument into a Boolean, but a double bang does the same thing with less typing.

Boolean("bang") === !!"bang"

Here’s a weird example:

!!new Boolean(false) // true
!!Boolean(false) // false

The reasoning is actually pretty simple. ‘Boolean(false)’ is equivalent to the primative false, which when double-banged, returns false. However, ‘new Boolean(false)’ is an object, and objects are truthy — when double-banged, it returns true, even if its argument is false.

Even given JavaScript’s many more falsey values, there still isn’t a huge need for the double bang. We can use it like we did in the Ruby example above to make sure that a function returns a Boolean. But there are other ways to do the same thing, and using the double bang may make your code harder to read and understand.

student.accepted = !!flatironEmail;

VS.

student.accepted = (flatironEmail != null);

All in all, I like the double bang, but I still can’t find much use for it.

--

--