Convert an Integer to Boolean in JavaScript

avatar

Borislav Hadzhiev

Last updated: Nov 3, 2021

banner

Photo from Unsplash

Convert an Integer to Boolean in JavaScript #

To convert an integer to a boolean in JavaScript, pass the integer to the Boolean object, e.g. Boolean(5). The Boolean converts integers with a value of zero to false and all other integers to true and returns the result.

index.js
console.log(Boolean(1)); // 👉️ true console.log(Boolean(0)); // 👉️ false console.log(Boolean(-0)); // 👉️ false console.log(Boolean(-1)); // 👉️ true

We used the Boolean object to convert integer values to boolean.

The only scenario where the Boolean object converts the integer to false is if the integer is equal to 0.

This is because the Boolean object converts truthy values to true and falsy values to false and 0 is a falsy value.

The falsy values in JavaScript are: false, null, undefined, 0, "" (empty string), NaN (not a number).

All other values are truthy and get converted to true when passed to the Boolean object.

An alternative approach is to use the double NOT (!!) operator.

To convert an integer to a boolean, use the double NOT (!!) operator, e.g. const bool = !!num. The double NOT (!!) operator converts integers with a value of zero to false and all other integers to true and returns the result.

index.js
console.log(!!1); // 👉️ true console.log(!!0); // 👉️ false console.log(!!-0); // 👉️ false console.log(!!-1); // 👉️ true

The double NOT (!!) operator is basically using the logical NOT (!) operator twice.

The logical NOT (!) operator converts a value to a boolean and inverts the result. Here are some examples.

index.js
console.log(!1); // 👉️ false console.log(!0); // 👉️ true

The integer 1 gets converted to a boolean true and then flipped, to get a value of false.

Zero is a falsy value but when converted to a boolean and flipped, we get a value of true.

To perform a pure boolean conversion, we have to flip the value again, by using a second logical NOT (!) operator.

index.js
console.log(!!1); // 👉️ true console.log(!!0); // 👉️ false
The first ! converts the integer to a boolean and flips the result. The second ! flips the boolean value, so we get a pure boolean conversion.

The double NOT (!!) operator is quite concise and does the same thing the Boolean object does, however it's a bit harder to read if you're not familiar with the logical NOT (!) operator.

Since we know that from all the integers only 0 converts to false, we can convert an integer to a boolean, by comparing it to 0.

index.js
const bool1 = 10 !== 0; console.log(bool1); // true const bool2 = 0 !== 0; console.log(bool2); // 👉️ false

We used the strict (!==) inequality operator to compare an integer to 0.

If the integer is not equal to 0, we get a value of true, otherwise, we get a value of false. This is exactly what we would expect if we had passed the integer to the Boolean object.

This also works in the edge case that the integer is equal to -0:

index.js
const bool3 = 0 !== -0; console.log(bool3); // 👉️ false

That's because 0 is equal to -0.

index.js
console.log(0 === -0); // 👉️ true

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.