JavaScript Binary to Decimal Conversion

Beginner
6 min read
Updated: Oct 2026
3 programs
Live preview

What Is Binary to Decimal Conversion?

Binary (base 2) uses only bits 0 and 1. Decimal (base 10) is the everyday number system. Conversion means reading each bit as a power of two and summing the contributions of every 1.

Remember
Rightmost bit = 2⁰ (value 1)
Next bit       = 2¹ (value 2)
Next bit       = 2² (value 4)
…

Example: 101010
  1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42
BinaryPlace values usedDecimal
10108 + 210
11118 + 4 + 2 + 115
10101032 + 8 + 242

How to Convert Binary to Decimal in JavaScript

Validate bits, then use a place-value loop, parseInt, or the doubling method.

  1. Read the binary string (only 0 and 1 allowed).
  2. Validate every character before converting.
  3. Sum place values (right-to-left), or call parseInt(bits, 2).
  4. Print or return the decimal result.
MethodIdeaTimeBest for
Place-value loopWalk right-to-left; add 2^power for each 1O(k)Interviews, learning place values
parseInt(s, 2)Parse the string as base 2 in one callO(k)Production code, short answers
Doubling methodLeft-to-right: result = result * 2 + bitO(k)Compact interview variant

Pseudocode

Pseudocode
function BinaryToDecimal(bits):
    if bits is empty:
        reject input

    decimal = 0
    power = 1                          // 2⁰

    for i from length(bits) - 1 down to 0:
        ch = bits[i]
        if ch is not '0' and not '1':
            reject input
        if ch == '1':
            decimal = decimal + power
        power = power * 2

    return decimal

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Valid bitsOnly 0 and 1
Place values1, 2, 4, 8, 16, 32, 64, 128, …
Start sideRightmost bit = 2⁰
Library callparseInt(bits, 2)
Classic check101010 → 42

Live Preview

Enter a binary string to see place values and the decimal result.

Digits 0 and 1 only (up to 31 bits). Tap a chip or type a value.

Result
Binary  = 101010
Places  = 32 + 8 + 2
Decimal = 42

JavaScript Programs

Start with the place-value loop, then parseInt, and finish with the doubling method. Each example has a Try it Yourself editor.

Example 1 — Place-Value Loop

Walk the string from right to left. Double the place value after each bit.

JavaScript
function binaryToDecimalManual(bits) {
  bits = bits.trim();
  if (!bits || /[^01]/.test(bits)) {
    throw new Error("Binary string must contain only 0 and 1");
  }

  let decimalValue = 0;
  let power = 1;

  for (let i = bits.length - 1; i >= 0; i--) {
    if (bits[i] === "1") {
      decimalValue += power;
    }
    power *= 2;
  }

  return decimalValue;
}

const binaryNumber = "101010";
const decimalNumber = binaryToDecimalManual(binaryNumber);
console.log("Binary: " + binaryNumber);
console.log("Decimal: " + decimalNumber);
Try It Yourself

How It Works

  1. Trim and reject empty or non-binary input before any math.
  2. Start at the rightmost bit with power = 1 (that is 20).
  3. If the bit is "1", add the current power. A "0" adds nothing.
  4. Double power and move one position left until the first character.
  5. Sample 101010 → 2 + 8 + 32 = 42.

Example 2 — parseInt Base 2

Validate the bits, then let the library parse the string as base 2.

JavaScript
function binaryToDecimalBuiltin(bits) {
  bits = bits.trim();
  if (!bits || /[^01]/.test(bits)) {
    throw new Error("Binary string must contain only 0 and 1");
  }
  return parseInt(bits, 2);
}

const binaryNumber = "101010";
console.log("Binary: " + binaryNumber);
console.log("Decimal: " + binaryToDecimalBuiltin(binaryNumber));
Try It Yourself

How It Works

  1. Reject empty strings and any character outside 0/1.
  2. parseInt(bits, 2) parses the string as a base-2 number.
  3. Always pass radix 2 — never rely on auto-detection.
  4. Returns a decimal number (for example "101010" → 42).
  5. Use the manual loop in interviews; use parseInt in real code.

Example 3 — Doubling Method

Walk left to right: multiply the running total by 2, then add the next bit.

JavaScript
function binaryToDecimalDoubling(bits) {
  bits = bits.trim();
  if (!bits || /[^01]/.test(bits)) {
    throw new Error("Binary string must contain only 0 and 1");
  }

  let total = 0;
  for (let i = 0; i < bits.length; i++) {
    total = total * 2 + (bits[i] === "1" ? 1 : 0);
  }
  return total;
}

const bits = "101010";
console.log("Binary: " + bits);
console.log("Decimal: " + binaryToDecimalDoubling(bits));
Try It Yourself

How It Works

  1. Start with total = 0 and scan from the leftmost bit.
  2. Each step: total = total * 2 + bit (Horner’s method).
  3. Same answer as place values — no separate power variable needed.
  4. Trace for 101010: 0→1→2→5→10→21→42.
  5. Compact, interview-friendly variant of the same idea.

Edge Cases & Pitfalls

Check these before calling the solution done.

Empty

Reject empty input

Return an error or throw — never convert silently.

0 / 1 only

Validate every character

2, spaces, or letters make the string invalid and can yield NaN.

Leading 0

Still valid

00101 equals 5. Leading zeros do not change the value.

Radix

Always pass 2 to parseInt

Omit the radix and older engines may misread leading zeros as octal.

Time and Space Complexity

ProgramTimeExtra space
Place-value / doublingO(k)O(1)
parseInt(s, 2)O(k)O(1)

k is the number of bits in the input string.

Key Takeaways

  • Place values: rightmost bit is 2⁰; only 1-bits add to the total.
  • Validate first: reject empty strings and any character outside 0/1.
  • Two options: manual loop, or parseInt(s, 2).
  • Test set: 101010 → 42; 1111 → 15; 0 → 0.

One line: sum powers of two for each 1-bit — or call parseInt(bits, 2).

Frequently Asked Questions

Sum powers of two for each 1-bit, or call parseInt(bits, 2). Both turn a base-2 string into a base-10 number.
It parses string s as a base-2 number and returns a decimal integer. Always pass the radix 2 explicitly.
Binary may contain only 0 and 1. Validation avoids wrong answers and NaN from parseInt.
No. Strings like 00101 are valid and equal 5 in decimal.
Both the manual loop and parseInt are O(k), where k is the number of bits. Extra space is O(1) beyond the input.
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.

Next: Common Divisors

Find every positive integer that divides two numbers evenly.

Common divisors tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful