There are a number of ways you could go about converting a boolean into an integer in JavaScript. Since JavaScript is a dynamically typed language and a variable can be of any type, for all the examples in this article, we'll assume that the values we expect from our variables are booleans only.

In cases where we don't know what the incoming value/type of a variable might be, if that variable value cannot be converted to number, `NaN`

is returned. This applies to all techniques that convert operands in an expression to a numeric value. This means that all methods explained in the article (except for the condition in the ternary operator) can potentially return `NaN`

when we're unsure of the variable always having a boolean value.

## Using the Ternary Operator

Perhaps the most basic and straightforward way is to use the ternary operator for converting a boolean into an integer. For example:

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 into an integer, `NaN`

is returned.

## Using the Unary Operator

The unary operator converts its operand into a number. With that information, if we use the unary operator with a boolean it should convert `true`

into `1`

and `false`

into `0`

. For example:

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

## Using Arithmetic Operators

When you perform an artithmetic operation on a boolean value, JavaScript internally converts `true`

to integer `1`

and `false`

to integer `0`

. Therefore, the following cases are valid and can be used to convert boolean into integer values:

### Using Multiplication:

true * 1 // 1 false * 1 // 0 // equivalent to 1 * 1; // 1 0 * 1; // 0

### Using Division:

true / 1; // 1 false / 1; // 0 // equivalent to 1 / 1; // 1 0 / 1; // 0

### Using Addition:

true + 0; // 1 false + 0; // 0 // equivalent to 1 + 0; // 1 0 + 0; // 0

### Using Subtraction:

true - 0; // 1 false - 0; // 0 // equivalent to 1 - 0; // 1 0 - 0; // 0

## Using Bitwise Operators

We can perform boolean to integer conversion by using bitwise operators as well, which invert the binary digits of an operand.

When performing bitwise operations with boolean values, JavaScript internally converts `true`

to `1`

and `false`

to `0`

.

### 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 we can see that:

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

If we replace the first operand with a boolean, we would get the same result as above because JavaScript converts booleans into 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 we can see that:

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

If we replace the first operand with a boolean, we would get the same result as above because JavaScript converts booleans into 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 we can see that:

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

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

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

## By Inverting Bits Twice Using Bitwise NOT

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 us the original number. For example:

~~a; // 1 ~~b; // 0

We can use this for converting booleans into integers:

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

This works because JavaScript will automatically convert the operand to a number when doing bitwise `NOT`

. This means that booleans will be converted into their numeric equivalents. For example, `true`

becomes `1`

and `false`

becomes `0`

.

## Using the Bitwise Shift Operators

Shifting bits simply means moving the binary bits of the first operand the specified number of bits either to the left (using the left shift operator `<<`

) or the right (using the right shift operator `>>`

).

For the sake of this article, we won't go into much detail about shifting bits in JavaScript; we'll go through just enough to help you understand the basic idea behind it. There are of course details that are beyond the scope of this article which you can read on about further if you're interested.

### Converting Booleans to Integers Using the Right Shift Operator:

Shifting `101`

(which is equivalent to decimal `5`

), for example, one times to the right would give us binary `10`

which equals decimal `2`

:

const a = 5; a >> 1; // 2

If instead of shifting the bits one time, suppose we shift it two times to the right, we would be left with `1`

which is equivalent to decimal `1`

:

a >> 2; // 1

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

a >> 0; // 5

And that is something we can use to convert a boolean into an integer with bit shifting:

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

This works because JavaScript internally converts boolean into 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

### Converting Booleans to Integers Using the Left Shift Operator:

If we 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 us 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 us the binary number `10100`

which is equivalent to decimal `20`

:

a << 2; // 20

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

a << 0; // 5

And that is something we can use to convert a boolean into an integer with bit shifting:

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

We skipped over some important bits related to left shift which is beyond the scope of this article. You can read up more on it if you're interested.

## Using the `Number`

Function

Boolean values can be converted to numbers using the `Number`

function like so:

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

This of course would be a bit slower in comparison to other methods in this article because it has the run-time complexity of a function.

## What's the Best Way of Converting Boolean to Integer in JavaScript?

The best method would really depend on your project. For instance, if you're working in a team environment, you should always strive to use methods that are easily readable for other programmers. On the other hand, when 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, knowing all the different ways, you can make an informed decision.

One important consideration, however, would be whether the variable you're converting into integer can have non-boolean values. If such is the case, then you might want to use the ternary operator as it won't return `NaN`

for values that can't be converted into an integer.

Hope you found this post useful. It was published (and was last revised ). Please show your love and support by sharing this post.