The Double Exclamation Operator (!!) in JavaScript - codingem.com

The Double Exclamation Operator (!!) in JavaScript

In JavaScript, the double exclamation operator converts an Object to Boolean. This happens such that “falsy” objects become false and “truthy” objects become true.

For example:

• !! 0 –> false
• !! null –> false
• !! undefined –> false
• !! 48 –> true
• !! “hello” –> true
• !! [1, 2, 3] –> true

This is a comprehensive guide on the double exclamation point operator.

About the Double Exclamation Operator: It’s Not an Operator

The double exclamation operator (!!) is actually the not (!) operator twice. So there is practically no double exclamation operator but rather a double not operator.

When you apply the not operator twice on a JavaScript object, here is what happens:

• The first not operator converts the object to an inverted boolean value.
• The second not operator inverts the inverted boolean value. In other words, this makes it the real boolean value of the object.

Example

For example, let’s apply the double explanation operation on a number:

```let bool = !!23
console.log(bool)```

Output:

`true`

Because a non-zero number is considered truthy in JavaScript, the above code:

• Converts the number 23 to “not 23” or false.
• Converts false to true.

But what does it mean for a value to be “truthy” or “falsy” in JavaScript?

The next chapter takes a deeper look at the concepts of truthiness and falseness.

“Truthy” and “Falsy” in JavaScript

In JavaScript, each and every object is associated with a boolean value. Another way to put it is that every JavaScript object is either true or false in a boolean context.

In JavaScript, each object is thus “truthy” or “falsy”. In other words, when converted to a boolean value:

• “Truthy” values become true.
• “Falsy” values become false.

But what types of objects are “truthy” and what objects are “falsy”?

In JavaScript, the following values are always “falsy”:

• false
• 0
• -0
• 0n (BigInt zero)
• “”
• null
• undefined
• NaN

Everything else is “truthy”!

You can test the truthiness by converting the values to booleans using the Boolean() object to perform the conversion.

For example, here are some “truthy” values:

```console.log(Boolean([0, 0, 0]))
console.log(Boolean("Hello"))
console.log(Boolean(15))```

Output:

```true
true
true```

And here are some “falsy” ones:

```console.log(Boolean(0))
console.log(Boolean(""))
console.log(Boolean(NaN))```

Output:

```false
false
false```

Why Use Double Exclamation (!!) in JavaScript?

So far you have learned that every JS value is associated with a boolean value. You also learned that the !! operator converts any object to a boolean value it would be in a boolean context.

Now you may wonder why would someone do this with a double not operator. There is a Boolean() class you can use too. The Boolean() casting would make the conversion more readable too.

Whether you use Boolean() or !! is up to a debate.

Some people consider using !! because it’s shorter than writing Boolean().

```!!0
Boolean(0)```

However, some people tend to favor Boolean() because it’s a more explicit way to do the typecasting.

Imagine not being familiar with the concept of !! and reading a line like this:

```if(!!str) {
....
}```

It wouldn’t make sense, would it?

It would be much cleaner if it was written like this:

```if(Boolean(str)) {
....
}```

In this expression, you can instantly tell that the writer of this piece of code wants to convert the string str to a boolean value.

To put it short, don’t use the !! operator to convert objects to Boolean. Instead, use the Boolean() object to do the conversion.

Wrap Up

Today you learned how the double exclamation mark operator works in JavaScript.

The double exclamation point converts any object to a boolean value.

To put it short, the double exclamation operator is not an actual operator. Instead, it’s a chain of two not operators. The double-not operator converts an object into the boolean value it would be in a boolean context.

• The first not operator converts the object into an inverted boolean value based on “truthiness” or “falsiness” of the object.
• The second not operator inverts the inverted boolean value.

This is possible because every object and value in JavaScript has an associated boolean value. In other words, every object is either “truthy” or “falsy”.

Most of the objects in JavaScript are truthy. However, values like NaN, 0, “”, and false are “falsy”.

But as it turns out, using the double-not operator is a rather obscure way to convert an object to a boolean. Instead, you should use the Boolean() object to make the conversion for easier understandability.