JavaScript

How to Convert Binary to Decimal in JavaScript

The computers are compatible with the binary form. Likewise, human beings easily understand numbers in decimal form. More specifically, the conversion of binary into decimal in JavaScript is of great aid in decoding the encoded data in binary form. This way, the converted data can be utilized effectively instead of wasted.

This blog will discuss the approaches to convert binary numbers into decimals using JavaScript.

How to Convert/Transform Binary to Decimal in JavaScript?

To convert binary digits into decimal numbers in JavaScript, apply the following methods:

    • parseInt()” method.
    • Math.pow()” method.

Method 1: Convert Binary to Decimal in JavaScript Using parseInt() Method

The “parseInt()” method parses a value as a string and gives the first integer. This method can convert the user-defined binary number into decimals by specifying the particular number system.

Syntax

parseInt(string, radix)

 
In this syntax:

    • string” corresponds to the value that needs to be converted.
    • radix” is an optional parameter for the type, which should be between 2 to 36.

Example

Let’s go through the following example:

<div style="text-align: center;">
 <h3 id="output"></h3>
</div>
<script>
 let binaryNumber = prompt("Enter Binary Number", "101011");
 if (binaryNumber != null) {
 output.innerHTML ="The decimal digit is " + parseInt(binaryNumber, 2);
 }
</script>

 
In the above code block:

    • Firstly, include the “<div>” element and adjust it to center. Also, include a heading to accumulate the resultant output.
    • In the JavaScript code, input the binary digit from the user via prompt.
    • After that, apply a check upon the input number, so it is not “null”.
    • Lastly, apply the “parseInt()” method to apply the required conversion by specifying the number system “2”.
    • Lastly, fetch the “<h3>” element such that the associated “innerHTML” property will display the resultant conversion as a heading.

Output


It can be observed that the user-defined binary number has been converted into a decimal number.

Method 2: Convert Binary to Decimal in JavaScript Using Math.pow() Method

The “Math.pow()” method assigns the value of the first parameter as the power of the second parameter. This method can be implemented to iterate through the passed binary number and apply the power upon the contained “1” in it.

Syntax

Math.pow(base, power)

 

    • base” refers to the base that is “2” in this case.
    • exponent” refers to the power of the base.

Example

Let’s overview the below-stated example:

function BinaryToDecimal(binary) {
 let decimal = 0;
 let binaryLength = binary.length;
 for (let i = binaryLength - 1; i >= 0; i--) {
  if (binary[i] == '1')
   decimal += Math.pow(2, binaryLength - 1 - i);
  }
  return decimal;
 }
console.log('The converted decimal value is:', BinaryToDecimal("101000"));

 
In the above code snippet:

    • Define a user-defined function named “BinaryToDecimal()”.
    • The function parameter points to the passed binary number that is to be converted/transformed.
    • In the function definition, store the length of the passed binary number via the “length” property.
    • In the “for” loop, iterate through the particular binary number from the last.
    • After that, apply the condition such that upon the occurrence of “1” in the binary number, the power of “2” is added to it using the “Math.pow()” method. However, the iterated “0” in it (binary number) remains the same.
    • Algorithm: 101000=> (2^ (6-1-3)) + (2^ (6-1-0) => 4+32 = 40.
    • Finally, invoke the defined function by passing the stated binary number as an argument.

Output


The desired conversion is achieved, as computed before.

Conclusion

To convert binary digits into decimal numbers in JavaScript, apply the “parseInt()” method or the “Math.pow()” method. The parseInt() method performs the desired conversion upon the user input value, and the Math.pow() method iterates through the passed binary numbers and adds the power of “2”. This blog discussed the approaches for converting binary digits into decimal numbers using JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.