Double Exclamation Point (!!) in JavaScript (and Why Boolean() is Better)

In JavaScript, the double exclamation mark (!!) converts any value to its corresponding boolean value.

console.log(!!0); // Output: false
Code language: JavaScript (javascript)

!! does this by first inverting the value using the logical NOT operator (a single exclamation mark), and then inverting it again using the second exclamation mark.

For example, in this code, the double exclamation mark first inverts the value of zero, which is 0, to true. It then inverts it again to false, which is the boolean value of 0.

The result will always be identical to using Boolean():

console.log(Boolean(0)); // Output: false
Code language: JavaScript (javascript)

In fact, due to the potential confusion that !! can cause, I recommend that you use Boolean() instead.

True or False?

In JavaScript, a value is considered to be “falsy” if it evaluates to false when converted to a boolean.

On the other hand, a value is considered to be “truthy” if it evaluates to true when converted to a boolean.

Here are some examples of falsy values in JavaScript:

  • The boolean value false
  • The number 0
  • The empty string ""
  • The special value null
  • The special value undefined

On the other hand, here are some examples of truthy values in JavaScript:

  • The boolean value true
  • Any non-zero number, such as 1, 2, or -5
  • Any non-empty string, such as "hello" or "world"
  • Any non-null object or array, such as { name: "John Doe" } or [1, 2, 3]
  • Empty objects and array, such as {} and []

When to Use the Double Exclamation Mark

The double exclamation mark is commonly used in JavaScript to explicitly convert a value to a boolean. This can be useful when a value needs to be a boolean.

For example, let’s say we wanted to write a function to check if a given object has a truthy foo property:

function objHasFoo(obj) { return obj && obj.foo; } function objHasFoo2(obj) { return !!(obj && obj.foo); } let obj = {foo: 'bar'}; console.log(objHasFoo(obj)); // Output: 'bar' console.log(objHasFoo2(obj)); // Output: true
Code language: JavaScript (javascript)

In this code, objHasFoo tests for obj && obj.foo but this actually returns the result of obj.foo which in this example is the string "bar". In objHasFoo2 we update the return statement to first cast the result of obj && obj.foo to a Boolean by using the double exclamation points.

As noted above, this could also be done with return Boolean(obj && obj.foo) instead.

Pitfalls of the Double Exclamation Mark

While the double exclamation mark can be useful in some cases, it can also be potentially confusing or even misleading if used incorrectly. This is because it relies on the double negation, which can be counterintuitive for some people.

This is why in general I recommend that you use Boolean() to cast a value to a boolean.

References

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *