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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>
             

    chevron_right

    
    

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

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>
             

    chevron_right

    
    

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

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>
             

    chevron_right

    
    

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

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>
             

    chevron_right

    
    

  • Output after clicking the button:
    Output Number()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.