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.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.