Home > Article > Web Front-end > How to convert boolean value to number in JavaScript?
In this tutorial, we will convert Boolean value to number in JavaScript. Boolean is the data type of the variable, and like other programming languages, JavaScript also supports this type.
TheBoolean data type contains only two values, true and false. In some cases, the programmer must convert a true or false value into a number. For example, use the strict equality operator to compare Boolean values with numeric variables.
Here, we have three ways to convert boolean values to numbers using different operators.
In JavaScript, the Number() function can be used to convert any variable into a number. We can also use it to convert boolean variables to numbers.
Users can follow the following syntax to convert Boolean values to numbers.
let bool = true; let result = Number(bool);
bool - This is a boolean variable and we need to convert it to a number.
In the following example, we use the Number() function of the JavaScript number library to convert two boolean values of true and false into numbers. Number()The function returns 1 for true value and 0 for false value.
<html> <head> </head> <body> <h2>Convert the Boolean to number in JavaScript.</h2> <h4>Convert the Boolean ( true / false ) respectively to number using <i> Number() </i> function.</h4> <div id="number1"></div> <div id = "number2"></div> </body> <script> var number1 = document.getElementById("number1"); var number2 = document.getElementById("number2"); let bool = true; let result = Number(bool); number1.innerHTML = result; number2.innerHTML = Number(false); </script> </html>
In this section, we will learn to convert Boolean values to numbers using bitwise OR and bitwise AND operators. When we bitwise OR a Boolean value with 0, it returns a numeric value.
Additionally, when the user performs a bitwise AND operation between 1 and any boolean value, it returns a respected numeric value.
Users can use bitwise operators to convert Boolean values to numbers according to the following syntax.
let bool = true; let result = bool | 0; // using the Bitwise OR operator let result = bool & 1; // using the Bitwise AND operator
In the example below we have taken two different examples of converting a boolean value to a number, one using the bitwise | operator and the other using the bitwise & operator .
<html> <head> </head> <body> <h2>Convert the Boolean to number in JavaScript.</h2> <h4>Converting the Boolean true to number using <i> Bitwise | </i> operator.</h4> <div id = "number1"></div> <h4>Converting the Boolean false to number using <i> Bitwise &</i> operator.</h4> <div id = "number2"></div> </body> <script> var number1 = document.getElementById("number1"); var number2 = document.getElementById("number2"); let bool = true; let result = bool | 0; number1.innerHTML = result; bool = false; number2.innerHTML = bool & 1; </script> </html>
This is the last method in this tutorial to convert a boolean value to a number. We will use addition, multiplication, and arithmetic operators. However, we can also use subtraction and division operators.
When we add or subtract 0 from a boolean value, it returns the numeric value associated with the boolean value. Likewise, when we multiply a Boolean value by 1, it returns the same result as addition.
Please use the multiplication and addition operators according to the following syntax.
let bool = true; let result = bool + 0; // using the Arithmetic + operator let result = bool * 1; // using the Arithmetic * operator
In the following example, we use addition and multiplication operators to convert Boolean values to numbers.
<html> <head> </head> <body> <h2>Convert the Boolean to number in JavaScript.</h2> <h4>Convert the Boolean false to number using <i> Arithmetic + </i> operator.</h4> <div id = "number1"></div> <h4>Convert the Boolean true to number using <i> Arithmetic * </i> operator.</h4> <div id = "number2"></div> </body> <script> var number1 = document.getElementById("number1"); var number2 = document.getElementById("number2"); let bool = false; let result = bool + 0; number1.innerHTML = result; bool = true; number2.innerHTML = bool * 1; </script> </html>
Users can convert Boolean values to integers using any of three methods. The first method calls built-in library functions and is slower than the second and third methods. Bitwise operations are the fastest operations in any programming language. So, the second method is the fastest method and can be easily used by users.
The above is the detailed content of How to convert boolean value to number in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!