Convert boolean result into number/integer in JavaScript - GeeksforGeeks

02 Jul, 2019
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 operatorUsing 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
Using unary + operator.
Using bitwise And (&) or bitwise Or ( | ) operator.
Using Number() function. It converts data type to number.
Syntax:var i = value ? 1 : 0;

var i = value ? 1 : 0;

Program:<!DOCTYPE html><html>  <body>    <center>        <h1 style="color:green">GeeksforGeeks</h1>        <h4>Click the button to change the boolean                                      value into number.</h4>        <script>            // Initializing boolvalue as true            var boolvalue = 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 operator            function myFunction() {                var i = boolvalue ? 1 : 0;                document.getElementById("result").innerHTML = i;            }        </script>  </center></body>  </html>         
<!DOCTYPE html><html>  <body>    <center>        <h1 style="color:green">GeeksforGeeks</h1>        <h4>Click the button to change the boolean                                      value into number.</h4>        <script>            // Initializing boolvalue as true            var boolvalue = 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 operator            function myFunction() {                var i = boolvalue ? 1 : 0;                document.getElementById("result").innerHTML = i;            }        </script>  </center></body>  </html>         
Output after clicking the button:
Syntax:var i = + boolvalue;

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 true            var boolvalue = 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 operator            function myFunction(){                var i = + boolvalue;                document.getElementById("result").innerHTML = i;            }        </script></body></html>         
<!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 true            var boolvalue = 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 operator            function myFunction(){                var i = + boolvalue;                document.getElementById("result").innerHTML = i;            }        </script></body></html>         
Output after clicking the button:
Syntax:var i = boolvalue & 1; // bitwise and
var j = boolvalue | 0; // bitwise or

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 true            var boolvalue = true;            // Initializing boolvalue2 as false            var boolvalue2 = 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 operator            function myFunction(){                var i = boolvalue & 1;                var j = boolvalue2 | 0;                document.getElementById("result").innerHTML = i;                document.getElementById("result2").innerHTML = j;            }        </script></body></html>         
<!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 true            var boolvalue = true;            // Initializing boolvalue2 as false            var boolvalue2 = 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 operator            function myFunction(){                var i = boolvalue & 1;                var j = boolvalue2 | 0;                document.getElementById("result").innerHTML = i;                document.getElementById("result2").innerHTML = j;            }        </script></body></html>         
Output after clicking the button:
Syntax:var i = Number(boolvalue);

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 true            var boolvalue = 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() function            function myFunction(){                var i = Number(boolvalue);                document.getElementById("result").innerHTML = i;            }        </script></body></html>         Output after clicking the button:
<!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 true            var boolvalue = 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() function            function myFunction(){                var i = Number(boolvalue);                document.getElementById("result").innerHTML = i;            }        </script></body></html>         
Output after clicking the button:
JavaScript-Misc
Picked
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to filter object array based on attributes?
How to get character array from string in JavaScript?
Lodash _.debounce() Method
Angular File Upload
How to remove duplicate elements from JavaScript Array ?
How to get selected value in dropdown list using JavaScript ?