JavaScript 将布尔结果转换为数字/整数
JavaScript中的 布尔值 表示两个值之一:true或false。
但是,如果要将存储布尔值的变量转换为整数 “0” 或 “1” ,可以使用多种方法。本文将介绍其中一些方法。
最常用的方法有:
- 使用三元或条件运算符
- 使用一元+运算符。
- 使用按位与(&)或按位或(|)运算符。
- 使用Number()函数。它将数据类型转换为数字。
使用三元或条件运算符:
- 语法:
var i = value ? 1 : 0;
- 示例:
<!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>
- 点击按钮后的输出:

使用一元 +运算符:
- 语法:
var i = +boolvalue;
- 程序:
<!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> 
- 点击按钮后的输出:

使用位与(&)或位或(|)操作符。
- 语法:
var i = boolvalue & 1; // 位与
var j = boolvalue | 0; // 位或
- 程序:
<!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> 
- 点击按钮后的输出:

使用Number()函数。它将数据类型转换为数字:
- 语法:
var i = Number(boolvalue);
- 程序:
<!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> 
点击按钮后的输出:

 极客教程
极客教程