How to Convert a Binary String to a Decimal Number in JavaScript?

To convert a binary string into a decimal number, you can simply use the Number.parseInt() method with 2 as the radix parameter like so:

const binary = '101101';
const decimal = Number.parseInt(binary, 2);

console.log(decimal); // 45

This would convert the number into its decimal equivalent.

If you have a really large binary number (that exceeds the Number.MAX_SAFE_INTEGER value), then you may get incorrect results when using the Number.parseInt() method. In such cases, manually converting the binary string to its decimal equivalent might be a better option.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.