How to convert an Integer to a Boolean in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Convert an Integer to Boolean in JavaScript

Use the Boolean() constructor to convert an integer to a boolean, e.g. Boolean(5).

The Boolean() constructor converts integers with a value of zero to false and all other integers to true.

index.js
console.log(Boolean(1)); // ๐Ÿ‘‰๏ธ true console.log(Boolean(0)); // ๐Ÿ‘‰๏ธ false console.log(Boolean(-0)); // ๐Ÿ‘‰๏ธ false console.log(Boolean(-1)); // ๐Ÿ‘‰๏ธ true const num = 1; const bool = Boolean(num); console.log(bool); // ๐Ÿ‘‰๏ธ true

convert integer to boolean in javascript

The code for this article is available on GitHub

We used the Boolean() constructor to convert integer values to boolean.

The Boolean() constructor only converts an integer to false if the integer is equal to 0.

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

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() constructor.

# Convert an Integer to a Boolean using the double NOT (!!) operator

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

The double NOT (!!) operator converts integers with a value of zero to false and all other integers to true.

index.js
console.log(!!1); // ๐Ÿ‘‰๏ธ true console.log(!!0); // ๐Ÿ‘‰๏ธ false console.log(!!-0); // ๐Ÿ‘‰๏ธ false console.log(!!-1); // ๐Ÿ‘‰๏ธ true const num = 0; const bool = !!num; console.log(bool); // ๐Ÿ‘‰๏ธ false

convert integer to boolean using double not

The code for this article is available on GitHub

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

The logical NOT (!) operator:

  1. Converts a value to a boolean
  2. Inverts the result
index.js
console.log(!1); // ๐Ÿ‘‰๏ธ false console.log(!0); // ๐Ÿ‘‰๏ธ true

The integer 1 gets converted to the 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 as the Boolean constructor.

However, it's a bit harder to read if you aren't familiar with the logical NOT (!) operator.

# Convert an Integer to a Boolean by comparing it to 0

Out of all integer values, only 0 converts to false.

Therefore, 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

convert integer to boolean by comparing it to 0

The code for this article is available on GitHub

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() constructor.

This also works in the edge case where 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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev