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 Binary to Decimal in JavaScript?
In this tutorial, we will learn to convert binary to decimal in JavaScript. A binary number is a string of '0' and '1' digits, representing numbers in base 2 format used in digital electronics.
Here are two different methods to convert binary numbers to decimal:
Using the parseInt() Method
The parseInt() method can extract numbers from strings and accepts a base parameter, making it perfect for binary conversion.
Syntax
let binary = "0101"; let decimal = parseInt(binary, 2);
Parameters
binary ? The binary number as a string
base ? The base of the input number (2 for binary)
Example
In this example, we use parseInt() with base 2 to convert different binary strings to decimal:
<html>
<head>
<title>Convert Binary to Decimal in JavaScript</title>
</head>
<body>
<h2>Convert Binary to Decimal using <i>parseInt()</i> method</h2>
<h4>The decimal of 0101 is:</h4>
<div id="decimal1"></div>
<h4>The decimal of 1100011110001 is:</h4>
<div id="decimal2"></div>
<script>
let decimal1 = document.getElementById("decimal1");
let decimal2 = document.getElementById("decimal2");
let binary = "0101";
decimal1.innerHTML = parseInt(binary, 2); // Base 2 for binary
binary = "1100011110001";
decimal2.innerHTML = parseInt(binary, 2);
</script>
</body>
</html>
The decimal of 0101 is: 5 The decimal of 1100011110001 is: 6385
Custom Function Using Math.pow()
We can create a custom function that manually converts binary to decimal using the mathematical principle: each binary digit represents a power of 2 based on its position.
For example, binary '110' converts to decimal as:
'110' = 1×2² + 1×2¹ + 0×2? = 4 + 2 + 0 = 6
Syntax
function binaryToDecimal(binary) {
let decimal = 0;
let length = binary.length;
for (let i = length - 1; i >= 0; i--) {
if (binary[i] == '1') {
decimal += Math.pow(2, length - 1 - i);
}
}
return decimal;
}
Example
This example demonstrates the custom function that iterates through each binary digit and adds the corresponding power of 2 when the digit is '1':
<html>
<head>
<title>Convert Binary to Decimal in JavaScript</title>
</head>
<body>
<h2>Convert Binary to Decimal using <i>Math.pow()</i> method</h2>
<h4>The decimal of 101000 is:</h4>
<div id="decimal1"></div>
<h4>The decimal of 11101010101 is:</h4>
<div id="decimal2"></div>
<script>
let decimal1 = document.getElementById("decimal1");
let decimal2 = document.getElementById("decimal2");
function binaryToDecimal(binary) {
let decimal = 0;
let length = binary.length;
for (let i = length - 1; i >= 0; i--) {
if (binary[i] == '1') {
decimal += Math.pow(2, length - 1 - i);
}
}
return decimal;
}
decimal1.innerHTML = binaryToDecimal("101000");
decimal2.innerHTML = binaryToDecimal("11101010101");
</script>
</body>
</html>
The decimal of 101000 is: 40 The decimal of 11101010101 is: 1877
Comparison
| Method | Code Complexity | Performance | Use Case |
|---|---|---|---|
parseInt() |
Simple | Fast | Recommended for most cases |
| Custom Function | Complex | Slower | Educational or when you need custom logic |
Conclusion
The parseInt() method with base 2 is the most efficient way to convert binary to decimal in JavaScript. Use the custom function approach when you need to understand the underlying mathematical process or require additional custom logic.
