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

Sanchithasr - Dec 31 '20 - - Dev Community

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.

!true // returns false
!false // returns true

isTrue = true // variable which is boolean
!isTrue // returns false
Enter fullscreen mode Exit fullscreen mode

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.

!!true // returns true
!!false // returns false

isTrue = true // variable which is boolean
!!isTrue // returns true
Enter fullscreen mode Exit fullscreen mode

We can take advantage of this using double-bang on non-boolean values as well which is pretty cool.

isNumber = 34 // variable which is not boolean
!!isNumber // returns true
Enter fullscreen mode Exit fullscreen mode

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.

var something = string;
 if (!!something) {
   console.log('This is truthy')
 } else {
   console.log('This is falsey') 
 }
Enter fullscreen mode Exit fullscreen mode

You can find more about it in the link here.

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.

var nothing = 0;
 if (!!nothing) {
   console.log('This is truthy')
 } else {
   console.log('This is falsey') 
 }
Enter fullscreen mode Exit fullscreen mode

You can find more about falsy values in the link here.

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

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
Enter fullscreen mode Exit fullscreen mode

And that sums it up!
Thank You!

. . . . . . . . . . . . . . . . . . . .
Terabox Video Player