What is the Double bang (!!) operator in JavaScript?

Tags: javascript, webdev, beginners
Published: 2020-12-31 05:15:32

Every value has *truth* or *false* values in JavaScript. For example, a `null` value has an associated _boolean_ value of *false*. Similarly `34` has an associated value of *true*. We can use this to cast a variable to *true* or *false* using the double bang operator.

Let’s dive deep into what it is and how it works.

The `!` in JavaScript, also called “bang”, is the logical “not” operator. If you place this operator in front of a *boolean* value, it will reverse the value, returning the opposite.

```javascript
!true // returns false
!false // returns true

isTrue = true // variable which is boolean
!isTrue // returns false
```
If the single bang returns the opposite *boolean* value, imagine what double-bang would return?

The associated *boolean* value. In other words, *true* or *false* according to whether it is __truthy__ or __falsy__ values.

Values that are associated with *boolean* _true_ are said to be __truthy__. Values that are associated with *boolean* *false* values are said to be __falsy__.

```javascript
!!true // returns true
!!false // returns false

isTrue = true // variable which is boolean
!!isTrue // returns true
```
We can take advantage of this using double-bang on non-boolean values as well which is pretty cool.

```javascript
isNumber = 34 // variable which is not boolean
!!isNumber // returns true
```

###Truthy values:

In JavaScript, a __truthy__ value is a value that is considered *true* when encountered in a _Boolean_ context.

The following values are few examples that are considered by JavaScript to be __truthys__:

* Object: `{}`
* Array: `[]`
* Not empty string: `"anything"`
* Number other than zero: `3.14`
* Date: `new Date();`

In the below example, variable `something` has the non-empty string value which has __truthy__ value in JavaScript so the console will print the first message.

```javascript
var something = ‘string’;
 if (!!something) {
   console.log('This is truthy')
 } else {
   console.log('This is falsey') 
 }
```
You can find more about it in the link [here](https://developer.mozilla.org/en-US/docs/Glossary/Truthy?source=post_page-----db5d8bb4acd6----------------------).

###Falsy values:
A __falsy__ value is a value that is considered *false* when encountered in a _Boolean_ context.

The following values are few of the examples that are considered by JavaScript to be __falsey__:

* Empty string: `""`
* `0`
* `null`
* `undefined`
* `NaN` and the list of _falsy_ values below.

In the below example, variable `nothing` has the `0` which has __falsy__ value in JavaScript so the console will print the second message.

```javascript
var nothing = 0;
 if (!!nothing) {
   console.log('This is truthy')
 } else {
   console.log('This is falsey') 
 }
```
You can find more about __falsy__ values in the link [here](https://developer.mozilla.org/en-US/docs/Glossary/Falsy?source=post_page-----db5d8bb4acd6----------------------).

Let us see how we can use it for type casting.

```javascript
function BankAccount(cash) {
this.cash = cash;
this.hasMoney = !!cash;
}

var myAccount = new BankAccount(80);
console.log(myAccount.cash); // expected result: 80
console.log(myAccount.hasMoney); // expected result: true

var emptyAccount = new BankAccount(0);
console.log(emptyAccount.cash); // expected result: 0
console.log(emptyAccount.hasMoney); // expected result: false
```

And that sums it up!
Thank You!