0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Binary to Decimal Number

Use parseInt(binaryString, 2) in JavaScript to convert a binary string to its decimal number equivalent.
📋

Examples

Input"101"
Output5
Input"1101"
Output13
Input"0"
Output0
🧠

How to Think About It

To convert binary to decimal, think of the binary number as a string of 0s and 1s. Each digit represents a power of 2, starting from the right with 2^0. We sum these powers where the digit is 1 to get the decimal value.
📐

Algorithm

1
Get the binary number as a string input.
2
Use a built-in function to interpret the string as base 2.
3
Return the resulting decimal number.
💻

Code

javascript
const binaryToDecimal = (binaryStr) => parseInt(binaryStr, 2);

console.log(binaryToDecimal("101")); // 5
console.log(binaryToDecimal("1101")); // 13
console.log(binaryToDecimal("0")); // 0
Output
5 13 0
🔍

Dry Run

Let's trace converting "101" to decimal using parseInt.

1

Input binary string

binaryStr = "101"

2

Convert using parseInt with base 2

parseInt("101", 2) calculates 1*2^2 + 0*2^1 + 1*2^0 = 4 + 0 + 1 = 5

3

Return decimal number

Returns 5

Binary DigitPower of 2Value
12^2 = 44
02^1 = 20
12^0 = 11
💡

Why This Works

Step 1: parseInt with radix 2

The parseInt function reads the string as a base 2 number when given 2 as the second argument.

Step 2: Binary to decimal conversion

Each binary digit is multiplied by its power of 2 position and summed to get the decimal value.

Step 3: Return decimal number

The function returns the decimal equivalent as a number type in JavaScript.

🔄

Alternative Approaches

Manual conversion with loop
javascript
function binaryToDecimalManual(binStr) {
  let decimal = 0;
  for (let i = 0; i < binStr.length; i++) {
    decimal = decimal * 2 + Number(binStr[i]);
  }
  return decimal;
}

console.log(binaryToDecimalManual("101")); // 5
This method shows the step-by-step calculation but is longer and less concise than parseInt.
Using Number constructor with '0b' prefix
javascript
const binaryToDecimalPrefix = (binStr) => Number('0b' + binStr);

console.log(binaryToDecimalPrefix("101")); // 5
This uses JavaScript's binary literal syntax but requires string concatenation.

Complexity: O(n) time, O(1) space

Time Complexity

The function processes each character of the binary string once, so it runs in linear time relative to the string length.

Space Complexity

It uses constant extra space since it only stores a few variables regardless of input size.

Which Approach is Fastest?

Using parseInt is fastest and simplest; manual loops are more educational but slower and longer.

ApproachTimeSpaceBest For
parseInt with radix 2O(n)O(1)Quick and simple conversion
Manual loop conversionO(n)O(1)Learning and understanding process
Number with '0b' prefixO(n)O(1)Using JavaScript binary literal syntax
💡
Use parseInt(binaryString, 2) for a quick and reliable binary to decimal conversion.
⚠️
Forgetting to specify the radix 2 in parseInt causes wrong results or NaN.