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
Binary
Place values used
Decimal
1010
8 + 2
10
1111
8 + 4 + 2 + 1
15
101010
32 + 8 + 2
42
Approach
How to Convert Binary to Decimal in JavaScript
Validate bits, then use a place-value loop, parseInt, or the doubling method.
Read the binary string (only 0 and 1 allowed).
Validate every character before converting.
Sum place values (right-to-left), or call parseInt(bits, 2).
Print or return the decimal result.
Method
Idea
Time
Best for
Place-value loop
Walk right-to-left; add 2^power for each 1
O(k)
Interviews, learning place values
parseInt(s, 2)
Parse the string as base 2 in one call
O(k)
Production code, short answers
Doubling method
Left-to-right: result = result * 2 + bit
O(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
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Valid bits
Only 0 and 1
Place values
1, 2, 4, 8, 16, 32, 64, 128, …
Start side
Rightmost bit = 2⁰
Library call
parseInt(bits, 2)
Classic check
101010 → 42
Try it
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
Code
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);
Reject empty strings and any character outside 0/1.
parseInt(bits, 2) parses the string as a base-2 number.
Always pass radix 2 — never rely on auto-detection.
Returns a decimal number (for example "101010" → 42).
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));