# What is Double Bang (!!) Operator in JavaScript and How it Works?

Find out the power of the double bang (!!) operator in JavaScript.

Learn how this shorthand technique can quickly convert truthy and falsy values to booleans, simplifying your code and improving its readability.

Discover the inner workings of the double bang operator and explore practical examples of when to use it in your projects.

Don’t miss out on this essential guide to enhance your JavaScript skills.

## What is double bang in JavaScript

The double bang (!!) operator in JavaScript is a quick method to turn a truthy or falsy value into a boolean.

It is also called as “not not” or “bang bang” operator, as it consists of two exclamation marks (!!) placed consecutively.

The first (!) “not” operator converts the value to a boolean and negates it, and the second (!) “not” operator negates it again, effectively converting the value to a boolean without changing its truthiness.

So, if you have a value that’s truthy (like a non-empty string or an object), using the double bang operator will turn it into true.

And if you have a value that’s falsy (like 0 or null), using the double bang operator will turn it into false.

In JavaScript and other languages that have truthy and falsy values, you can use the double bang operator to ensure you’re working with a boolean.

## What is truthy and falsy in JavaScript?

A truthy value is something that gets treated as true when it’s seen in a situation where we need a true or false answer.

By default, everything is treated as truthy, unless we specifically say it’s falsy.

All values are truthy except:

✔ false

✔ 0

✔ an empty string (“”)

✔ null

✔ undefined

✔ and NaN

In JavaScript, when it comes to Booleans, there’s something called type coercion happening.

On the other hand, in JavaScript, a falsy value is a value that is considered false when encountered in a Boolean context.

There are only a few falsy values in JavaScript: false, 0, -0, 0n, “”, null, undefined, and NaN.

## When to use double bang operator?

We can use the double-bang (!!) in JavaScript when it really needs a clear yes or no answer.

Also, if you’re giving a value to someone else and you’re not sure if they’ll treat it as a clear yes or no, the double-bang can help.

## How to use the bang operator in JavaScript?

Here’s an example of how to use the double bang (!!) operator in JavaScript:

``````let sampleString = "Welcome to Itsourcecode,!";
let sampleBoolean = !!sampleString; ✅

console.log(sampleBoolean); ``````

We have a variable sampleString that contains a non-empty string.

When we use the double bang (!!) operator on sampleString, it converts the truthy value of the non-empty string into the boolean value true.

We then assign this boolean value to the variable sampleBoolean and log it to the console.

Output:

``true``

Here’s another example:

``````let sampleNumber = 0;
let sampleBoolean = !!sampleNumber; ✅

console.log(sampleBoolean);``````

In our second example code, we have a variable sampleNumber that contains the value 0.

When we use the double bang operator on sampleNumber, it converts the falsy value of 0 into the boolean value false.

Then, we assign this boolean value to the variable sampleBoolean and log it to the console.

Output:

``false``

## Conclusion

In conclusion, this article discusses the effectiveness of the double bang (!!) operator in JavaScript for converting truthy and falsy values into booleans.

The double bang (!!) operator serves as a concise way to enhance code readability and simplify conditional logic.

We are hoping that this article provides you with enough information that helps you understand the JavaScript double bang.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading itsourcecoders 😊.