How to Convert a Boolean to an Integer in JavaScript?

In JavaScript, you can convert a boolean true or false to their numeric equivalents of 1 or 0 (respectively), in the following ways:

You could determine the best method for you on a per-project basis. For example, if you're working in a team environment, then you should always strive to use methods that are easily readable for other programmers. On the other hand, if you're developing a library of your own, etc. (where micro-optimizations might help) the best method might be the one that's the fastest. Therefore, by knowing different ways, you can make an informed decision.

All methods covered in the post (except for the condition in the ternary operator) can potentially return NaN when you're unsure of the variable always having a boolean value.

Using Number()

Boolean values can be converted to numbers using the Number() function like so:

Number(true); // 1
Number(false); // 0

This might be a bit slower in comparison to other methods because it has the run-time complexity of a function. However, it is perhaps the most readable method of explicitly converting a boolean to its numeric equivalent, where the intention is immediately clear by reading the code.

Using Ternary Operator

You can simply use the boolean in a ternary condition to return 1 when the boolean is true and 0 otherwise:

true ? 1 : 0; // 1
false ? 1 : 0; // 0

The conditional part in the ternary can evaluate variables of any type and would safely return 1 or 0 even for variables that may have non-boolean values. This is an important distinction to make with other methods where, when an operand in an expression cannot be converted to an integer, NaN is returned.

Using Unary Operators

A unary operation is an operation with only one operand. In JavaScript, you can use the following unary operators to convert a boolean to an integer:

Using Unary Plus Operator:

The unary plus operator (+) converts its operand to a number. Therefore, if you use the unary operator with a boolean it would convert true to 1 and false to 0. For example:

+true; // 1
+false; // 0

Using Bitwise NOT Operator:

Bitwise NOT (~) inverts the bits of its operand. For example inverting the bits of decimal 0 and 1 have the following results:

const a = 1; // 32-bit binary equivalent: 00000000000000000000000000000001

~a; // -2 (32-bit binary equivalent: 11111111111111111111111111111110)

const b = 0; // 32-bit binary equivalent: 00000000000000000000000000000000
~b; // -1 (32-bit binary equivalent: 11111111111111111111111111111111)

And inverting the results again would give you the original number. For example:

~~a; // 1

~~b; // 0

You can use this for converting booleans to integers:

~~true; // 1
~~false; // 0

This works because JavaScript automatically converts the operand to a number when doing bitwise NOT. This means that booleans will be converted to their numeric equivalents. For example, true becomes 1 and false becomes 0.

The result can be visualized in terms of the following truth table for the double NOT operation:

x ~x ~~x
true (or 1) 0 1
false (or 0) 1 0

Using Arithmetic Operators

When you perform an arithmetic operation on a boolean value, JavaScript internally converts true to integer 1 and false to integer 0. Therefore, the following can be used to convert boolean to integer values:

Although, it's possible to do this, you should generally avoid using these just for the sole purpose of converting a boolean to an integer as it is not very readable.

Using Addition:

You could add 0 to true or false, which would coerce the value to a number first, and then return the result of the addition:

true + 0; // 1
false + 0; // 0

// equivalent to
1 + 0; // 1
0 + 0; // 0

Using Subtraction:

You could subtract 0 from true or false, which would coerce the value to a number first, and then return the result of the subtraction:

true - 0; // 1
false - 0; // 0

// equivalent to
1 - 0; // 1
0 - 0; // 0

Using Multiplication:

You could multiply a boolean with 1, which would coerce it to a number first, and then return the result of the multiplication:

true * 1 // 1
false * 1 // 0

// equivalent to
1 * 1; // 1
0 * 1; // 0

Using Division:

You could divide a boolean value with 1, which would coerce it to a number first and then return the result of the division:

true / 1; // 1
false / 1; // 0

// equivalent to
1 / 1; // 1
0 / 1; // 0

Using Bitwise Operators

You can perform boolean to integer conversion by using bitwise operators as well, for example, in the following ways:

When performing bitwise operations with boolean values, JavaScript internally converts true to 1 and false to 0.

Although, it's possible to do this, you should generally avoid using these just for the sole purpose of converting a boolean to an integer as it is not very readable.

Bitwise AND:

The bitwise AND operator (&) returns 1 when the corresponding bits of both operands are 1 and returns 0 otherwise. The following truth table demonstrates this:

x y x & y
0 0 0
0 1 0
1 0 0
1 1 1

From the table above you can see that:

1 & 1; // 1
0 & 1; // 0

If you replace the first operand with a boolean, you would get the same result as above because JavaScript converts booleans to their numeric equivalents when performing bitwise operations:

true & 1; // 1
false & 1; // 0

Bitwise OR:

The bitwise OR operator (|) returns 1 when bits of one of the operands is 1, which means it returns 0 only when both operands are 0. The following truth table demonstrates this:

x y x | y
0 0 0
0 1 1
1 0 1
1 1 1

From the table above you can see that:

1 | 0; // 1
0 | 0; // 0

If you replace the first operand with a boolean, you would get the same result as above because JavaScript converts booleans to their numeric equivalents when performing bitwise operations:

true | 0; // 1
false | 0; // 0

Bitwise XOR:

The bitwise XOR operator (^) returns 1 when the corresponding bits are different and 0 when the corresponding bits are the same. The following truth table demonstrates this:

x y x ^ y
0 0 0
0 1 1
1 0 1
1 1 0

From the table above you can see that:

1 ^ 0; // 1
0 ^ 0; // 0

If you replace the first operand with a boolean, you would get the same result as above because JavaScript converts booleans to their numeric equivalents when performing bitwise operations:

true ^ 0; // 1
false ^ 0; // 0

Using Bitwise Shift Operators

You can shift bits to convert booleans to integers in the following ways:

Either of these would move the binary bits of the first operand the specified number of bits either to the left or the right.

Although, it's possible to do this, you should generally avoid using these just for the sole purpose of converting a boolean to an integer as it is not very readable.

Using Right Shift Operator:

Shifting 101 (which is equivalent to decimal 5), for example, one times to the right would give you binary 10, which equals decimal 2:

const a = 5;

a >> 1; // 2

If instead of shifting the bits one time, suppose you shift it two times to the right, you would be left with 1 which is equivalent to decimal 1:

a >> 2; // 1

And, if you shift bits zero times, then you're simply left with the number itself:

a >> 0; // 5

That's something you can use to convert a boolean to an integer with bit shifting:

true >> 0; // 1
false >> 0; // 0

This works because JavaScript internally converts boolean to their numeric equivalents. This particular case would work with >>> (unsigned right shift) all the same as >> (signed right shift):

true >>> 0; // 1
false >>> 0; // 0

Using Left Shift Operator:

If you were to shift bits to the left, specified number of zero bits are added to the right hand side. For example, shifting 101 (which is equivalent to decimal 5) one times to the left would give you binary 1010, which equals to decimal 10:

const a = 5;

a << 1; // 10

And shifting two bits to the left of decimal five would give you the binary number 10100, which is equivalent to decimal 20:

a << 2; // 20

Similarly, if you shift bits zero times, then you're simply left with the number itself:

a << 0; // 5

That's something you can use to convert a boolean to an integer with bit shifting:

true << 0; // 1
false << 0; // 0

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.