Convert an Integer to Boolean in JavaScript

avatar

Borislav Hadzhiev

Wed Nov 03 20212 min read

banner

Photo by Tamara Bellis

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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee