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

Sanchitha SR
Dec 30, 2020 · 3 min read

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.

Image for post
Image for post

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

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

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

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')
}

You can find more about it in the link below.

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 falseys:

  • Empty string: ""
  • 0
  • null
  • undefined
  • NaNand 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')
}

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

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

And that sums it up!

Thank You!

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store