- Using ternary or conditional operator
- Using unary + operator.
- Using bitwise And (&) or bitwise Or ( | ) operator.
- Using Number() function. It converts data type to number.
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:
- Using ternary or conditional operator:
- Syntax:
var i = value ? 1 : 0;
- Program:
<!DOCTYPE html><html><body><center><h1 style="color:green">GeeksforGeeks</h1><h4>Click the button to change the booleanvalue into number.</h4><script>// Initializing boolvalue as truevarboolvalue =true</script><button onclick="myFunction()">Change</button><p>The number value of the variable is :</p><p id="result"></p><script>// JavaScript program to illustrate boolean// conversion using ternary operatorfunctionmyFunction() {vari = boolvalue ? 1 : 0;document.getElementById("result").innerHTML = i;}</script></center></body></html>chevron_rightfilter_none - Output after clicking the button:

- Using unary + operator:
- Syntax:
var i = + boolvalue;
- Program:
<!DOCTYPE html><html><body><center><h1 style="color:green">GeeksforGeeks</h1><p>Click the button to change the boolean value.</p><script>// Initializing boolvalue as truevarboolvalue =true;</script><button onclick="myFunction()">Change</button><p>The value of the variable is now:</p><p id="result"></p><script>// JavaScript program to illustrate boolean// conversion using unary operatorfunctionmyFunction(){vari = + boolvalue;document.getElementById("result").innerHTML = i;}</script></body></html>chevron_rightfilter_none -
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:
<!DOCTYPE html><html><body><center><h1 style="color:green">GeeksforGeeks</h1><p>Click the button to change the boolean value.</p><script>// Initializing boolvalue as truevarboolvalue =true;// Initializing boolvalue2 as falsevarboolvalue2 =false;</script><button onclick="myFunction()">Change</button><p>The value of the variable 1 is now:</p><p id="result"></p><p>The value of the variable 2 is now:</p><p id="result2"></p><script>// JavaScript program to illustrate boolean// conversion using bitwise operatorfunctionmyFunction(){vari = boolvalue & 1;varj = boolvalue2 | 0;document.getElementById("result").innerHTML = i;document.getElementById("result2").innerHTML = j;}</script></body></html>chevron_rightfilter_none - Output after clicking the button:

- Using Number() function. It converts data type to number.:
- Syntax:
var i = Number(boolvalue);
- Program:
<!DOCTYPE html><html><body><center><h1 style="color:green">GeeksforGeeks</h1><p>Click the button to change the boolean value.</p><script>// Initializing boolvalue as truevarboolvalue =true;</script><button onclick="myFunction()">Change</button><p>The value of the variable is now:</p><p id="result"></p><script>// JavaScript program to illustrate boolean// conversion using Number() functionfunctionmyFunction(){vari = Number(boolvalue);document.getElementById("result").innerHTML = i;}</script></body></html>chevron_rightfilter_none
Output after clicking the button:
Recommended Posts:
- How to Combine multiple elements and append the result into a div using JavaScript ?
- How to get negative result using modulo operator in JavaScript ?
- JavaScript | Convert a string to boolean
- Converting a string variable into Boolean, Integer or Float type in Golang
- jQuery | event.result Property with Examples
- Lodash _.result() Method
- Underscore.js _.result() Function
- How to convert string to boolean in PHP?
- Java Program to convert boolean to integer
- JavaScript Boolean valueOf() Method
- JavaScript Boolean toString() Method
- JavaScript Boolean
- JavaScript Boolean prototype Constructor
- Sort an array of objects using Boolean property in JavaScript
- JavaScript Boolean constructor Property
- JavaScript | Boolean and dataView Complete Reference
- How to generate a random boolean using JavaScript ?
- How to toggle a boolean using JavaScript ?
- Convert string into date using JavaScript
- How to convert string into float in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


