Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert Boolean to Number in JavaScript?
In JavaScript, Boolean values (true and false) can be converted to numbers (1 and 0 respectively) using several methods. This conversion is useful when performing mathematical operations or comparisons that require numeric values.
JavaScript provides multiple approaches to convert Boolean values to numbers. Let's explore three effective methods with examples.
Using the Number() Function
The Number() function is the most explicit and readable way to convert Boolean values to numbers. It converts true to 1 and false to 0.
Syntax
let result = Number(booleanValue);
Example
<html>
<head>
</head>
<body>
<h2>Convert Boolean to Number using Number() Function</h2>
<div id="result1"></div>
<div id="result2"></div>
</body>
<script>
let bool1 = true;
let bool2 = false;
let number1 = Number(bool1);
let number2 = Number(bool2);
document.getElementById("result1").innerHTML = "Number(true) = " + number1;
document.getElementById("result2").innerHTML = "Number(false) = " + number2;
</script>
</html>
Number(true) = 1 Number(false) = 0
Using Bitwise Operators
Bitwise operators provide a fast way to convert Boolean to numbers. The Bitwise OR (|) with 0 and Bitwise AND (&) with 1 both effectively convert Boolean values to their numeric equivalents.
Syntax
let result1 = booleanValue | 0; // Bitwise OR let result2 = booleanValue & 1; // Bitwise AND
Example
<html>
<head>
</head>
<body>
<h2>Convert Boolean to Number using Bitwise Operators</h2>
<div id="bitwise1"></div>
<div id="bitwise2"></div>
</body>
<script>
let bool1 = true;
let bool2 = false;
let result1 = bool1 | 0; // Using Bitwise OR
let result2 = bool2 & 1; // Using Bitwise AND
document.getElementById("bitwise1").innerHTML = "true | 0 = " + result1;
document.getElementById("bitwise2").innerHTML = "false & 1 = " + result2;
</script>
</html>
true | 0 = 1 false & 1 = 0
Using Arithmetic Operators
Arithmetic operators can also convert Boolean values to numbers. Adding 0 or multiplying by 1 are common techniques that leverage JavaScript's automatic type coercion.
Syntax
let result1 = booleanValue + 0; // Addition let result2 = booleanValue * 1; // Multiplication
Example
<html>
<head>
</head>
<body>
<h2>Convert Boolean to Number using Arithmetic Operators</h2>
<div id="arithmetic1"></div>
<div id="arithmetic2"></div>
</body>
<script>
let bool1 = false;
let bool2 = true;
let result1 = bool1 + 0; // Addition with 0
let result2 = bool2 * 1; // Multiplication with 1
document.getElementById("arithmetic1").innerHTML = "false + 0 = " + result1;
document.getElementById("arithmetic2").innerHTML = "true * 1 = " + result2;
</script>
</html>
false + 0 = 0 true * 1 = 1
Performance Comparison
| Method | Readability | Performance | Use Case |
|---|---|---|---|
Number() |
High | Moderate | General purpose, clear intent |
| Bitwise operators | Low | Fastest | Performance-critical code |
| Arithmetic operators | Moderate | Fast | Mathematical contexts |
Conclusion
All three methods effectively convert Boolean values to numbers, with true becoming 1 and false becoming 0. Choose Number() for clarity, bitwise operators for performance, or arithmetic operators for mathematical operations.
