Every value has an associated boolean, true or false, value in JS. For example, a null value has an associated boolean value of false. A string value, such as abc has an associated boolean value of true.

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.

A full list of truthy values can be found here:

Truthy

A full list of falsy values can be found here:

Falsy

A single “!” symbol in javascript, also called a “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.



If a bang (“!”) returns the opposite truthy value, what would a bang executed twice on a value do?

const x = true; // Associated with true.
const a = !x; // The returns the opposite of 'x', false.
const b = !a; // The ! takes value 'a' of false and reverses it back to true.

!!true // Evaluates to true.
!!false // Evaluates to false.

So, running a bang twice determines the opposite of value, and then returns the opposite of that.

With non-boolean values, it is kind of cool to see what the double bang does:

const x = 'abc'; // Associated with true.
const a = !x; // The ! determines x is associated with true and returns the opposite, false.
const b = !a; // The ! takes value 'a' of false and reverses it to true.

!!'abc' // Evaluates to true.
!!null // Evaluates to false.

Knowing the associated boolean value of any given value is helpful if you want to quickly reduce a value to a boolean:

function BankAccount(cash) {
this.cash = cash;
this.hasMoney = !!cash;
}
var account = new BankAccount(100.50);
console.log(account.cash); // 100.50
console.log(account.hasMoney); // true

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

In this case, if an account.cash value is greater than zero, the account.hasMoney will be true.

Let’s see another example:

const userA = getUser('existingUser'); // { name: Abel, status: 'lit' }
const userB = getUser('nonExistingUser'); // null

const userAExists = !!userA; // true
const userBExists = !!userB; // false

featured_image
About the Author

Abel Masila

Software Engineer at Andela

More Insights

November 21, 2019

Javascript convert to boolean using !!(double bang)operator

Abel Masila

Every value has an associated boolean, true or false, value in JS. For example, a null value has an associated boolean value of false. A string value, such as abc has an associated boolean value of true.

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.

A full list of truthy values can be found here:

Truthy

A full list of falsy values can be found here:

Falsy

A single “!” symbol in javascript, also called a “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.



If a bang (“!”) returns the opposite truthy value, what would a bang executed twice on a value do?

const x = true; // Associated with true.
const a = !x; // The returns the opposite of 'x', false.
const b = !a; // The ! takes value 'a' of false and reverses it back to true.

!!true // Evaluates to true.
!!false // Evaluates to false.

So, running a bang twice determines the opposite of value, and then returns the opposite of that.

With non-boolean values, it is kind of cool to see what the double bang does:

const x = 'abc'; // Associated with true.
const a = !x; // The ! determines x is associated with true and returns the opposite, false.
const b = !a; // The ! takes value 'a' of false and reverses it to true.

!!'abc' // Evaluates to true.
!!null // Evaluates to false.

Knowing the associated boolean value of any given value is helpful if you want to quickly reduce a value to a boolean:

function BankAccount(cash) {
this.cash = cash;
this.hasMoney = !!cash;
}
var account = new BankAccount(100.50);
console.log(account.cash); // 100.50
console.log(account.hasMoney); // true

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

In this case, if an account.cash value is greater than zero, the account.hasMoney will be true.

Let’s see another example:

const userA = getUser('existingUser'); // { name: Abel, status: 'lit' }
const userB = getUser('nonExistingUser'); // null

const userAExists = !!userA; // true
const userBExists = !!userB; // false

featured_image
About the Author

Abel Masila

Software Engineer at Andela

Thanks for subscribing!

 

More Insights

Are You Ready? Get Ahead of the Curve in 2021.

2020 was supposed to be the year of perfect vision (get it, “20-20”?). Instead, it has been a b...

22_September_2020

66% of Engineering Leaders Indicate Remote Work is Here to Stay

CTOs who embrace remote work can unlock a global talent pool New York, NY (September 22, 2020) A...

22_September_2020

Meet Andela at Plato Elevate 2020

Plato's annual Elevate conference is going virtual for 2020 on September 24-25, and it promises to ...

17_September_2020

Upskill with Andela Online Technical Workshops

As Covid-19 closed offices and in-person meetups and hackathons disappeared, Andela began hosting a...

15_September_2020

Andela Hires Sachin Bhagwata as Vice President of Enterprise

Software industry leader brings enterprise expertise to lead growth efforts NEW YORK, Sept. 09, ...

9_September_2020

It’s Not Just About the Code: How to Find Great Developers that are Great Leaders

The move to all-remote engineering has exposed a need that was less visible but growing among softw...

18_August_2020

Partners

Tap into a global talent pool and hire the “right” developers in days, not months.

Developers

Accelerate your career by working with high-performing engineering teams around the world.

BECOME A DEVELOPER

Hire Developers

We take great pride in matching our developers with the best partners. Tell us about your team below!

preloader_image

Thank you for your interest

A member of our team will reach out to you soon.