Home > Web Front-end > HTML Tutorial > How to convert boolean value to number in JavaScript?

How to convert boolean value to number in JavaScript?

WBOY
Release: 2023-08-26 13:21:16
forward
1227 people have browsed it

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.

The

Boolean 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.

Use the Number() function

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.

grammar

Users can follow the following syntax to convert Boolean values ​​to numbers.

let bool = true;
let result = Number(bool);
Copy after login

parameter

  • bool - This is a boolean variable and we need to convert it to a number.

Example

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>
Copy after login

Use bitwise operators

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.

grammar

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
Copy after login

Example

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>
Copy after login

Use arithmetic operators

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.

grammar

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
Copy after login

Example

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template