Thursday, September 4, 2025
HomeLanguagesJavascriptHow to convert Number to Boolean in JavaScript ?

How to convert Number to Boolean in JavaScript ?

We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer “0” or “1” into Boolean Value i.e. “false” or “true”. 

Approach 1: Boolean() method: Boolean function returns the boolean value of the variable. It can also be used to find the boolean result of a condition, expression, etc

Syntax:  

Boolean(variable/expression)

Example:  This example converts a number into a boolean.

HTML




<!DOCTYPE html>
<html>
<body>
    <center>
        <h1 style="color:green">
            neveropen
        </h1>
 
        <h4>
            Click the button to change the
            number value into boolean.
        </h4>
 
        <button onclick="myFunction()">Change</button>
 
        <p>The number value of the variable is :</p>
 
        <p id="result"></p>
 
        <script>
            // Initializing boolvalue as true
            var numvalue = 1;
             
            // JavaScript program to illustrate boolean
            // conversion using ternary operator
            function myFunction() {
             
                document.getElementById("result")
                    .innerHTML = Boolean(numvalue);
            }
        </script>
    </center>
</body>
</html>


Note: If the above Boolean() method is called as Boolean(!numvalue), it shows the result as “false“. Similarly, if it is called Boolean(!!numvalue), it gives the result as “true“.

Output:

 

Approach 2: Using double NOT(!!) operator: This operator converts numbers with the value of zero to false and all other numbers to true.

Example: In this example, we will use the double NOT operator to convert the number into a boolean value.

HTML




<!DOCTYPE html>
<html>
<body>
    <center>
        <h1 style="color:green">
            neveropen
        </h1>
 
        <h4>
            Click the button to change the
            number value into boolean.
        </h4>
 
        <button onclick="myFunction()">Change</button>
 
        <p>The number value of the variable is :</p>
 
        <p id="result"></p>
 
        <script>
            // Initializing boolvalue as true
            var numvalue = (!!1)
 
            // JavaScript program to illustrate boolean
            // conversion using ternary operator
            function myFunction() {
 
                document.getElementById("result")
                    .innerHTML = (numvalue);
            }
        </script>
    </center>
</body>
</html>


Output:

 

RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS