GeeksforGeeks App
Open App
Browser
Continue

# Convert boolean result into number/integer in JavaScript

A JavaScript boolean represents one of two values: true or false.
However, if one wants to convert a variable that stores boolean value, into integer “0” or “1”, they can do so using multiple approaches. We will look into some of them in this article.

The most popular methods are:

1. Using ternary or conditional operator
2. Using unary + operator.
3. Using bitwise And (&) or bitwise Or ( | ) operator.
4. Using Number() function. It converts data type to number.
Using ternary or conditional operator:

• Syntax:
```var i = value ? 1 : 0;
```
• Program:

 ````` ` ```    ``
``        ``

GeeksforGeeks

``        ``

Click the button to change the boolean ``                                     ``value into number.

``        ````        ````        ``

The number value of the variable is :

``        ``

``        ````  ``
```` ` ```        `

• Output after clicking the button:
Using unary + operator:

• Syntax:
```var i = + boolvalue;
```
• Program:

 ```````    ``
``        ``

GeeksforGeeks

``        ``

Click the button to change the boolean value.

``        ````        ````        ``

The value of the variable is now:

``        ``

``        ````````        `

• Output after clicking the button:
Using bitwise And (&) or bitwise Or ( | ) operator.

• Syntax:
```var i = boolvalue & 1; // bitwise and
var j = boolvalue | 0; // bitwise or
```
• Program:

 ```````    ``
``        ``

GeeksforGeeks

``        ``

Click the button to change the boolean value.

``        ````        ````        ``

The value of the variable 1 is now:

``        ``

``        ``

The value of the variable 2 is now:

``        ``

``        ````````        `

• Output after clicking the button:
Using Number() function. It converts data type to number.:

• Syntax:
```var i = Number(boolvalue);
```
• Program:

 ```````    ``
``        ``

GeeksforGeeks

``        ``

Click the button to change the boolean value.

``        ````        ````        ``

The value of the variable is now:

``        ``

``        ````````        `

• Output after clicking the button:

My Personal Notes arrow_drop_up