Decimal and Binary are two number systems used to represent numbers. The binary is a base-2 number system that uses only two digits, 0 and 1, whereas decimal is a base-10 number system that uses 10 digits, 0 to 9. Binary numbers are commonly used in computer programming and digital electronics, while decimal numbers are used in everyday life.

Converting binary to decimal is a common task in programming.

We Will Explore How to Convert Binary to a Decimal Using JavaScript

There are different ways to convert binary to decimal, but one of the most common methods is to use the positional notation system. In this system, each digit in a number represents a certain value based on its position. For example, in the decimal number system, the digit in the one’s place represents a value of 1, the digit in the tens place represents a value of 10, the digit in the hundreds place represents a value of 100, and so on.

check Decimal Table

Similarly, in the binary number system, the digit in the rightmost position (also known as the least significant bit) represents a value of 1, the digit in the second position from the right represents a value of 2, the digit in the third position from the right represents a value of 4, the digit in the fourth position from the right represents a value of 8, and so on. Each digit in a binary number represents a power of 2.

Let’s try to understand this concept better. Consider the binary number 101101. To convert this binary number to decimal, we need to find the decimal equivalent of each binary digit and add them up. We can use the positional notation system to find the decimal equivalent of each binary digit.

The rightmost digit in the binary number 101101 represents a value of 1, so its decimal equivalent is 1. The second digit from the right represents a value of 2, so its decimal equivalent is 2. The third digit from the right represents a value of 4, so its decimal equivalent is 4. The fourth digit from the right represents a value of 8, so its decimal equivalent is 8. The fifth digit from the right represents a value of 16, so its decimal equivalent is 16. And the leftmost digit represents a value of 32, so its decimal equivalent is 32.

Now, we can add up the decimal equivalents of each binary digit to get the decimal equivalent of the binary number.

1 x 1 + 0 x 2 + 1 x 4 + 1 x 8 + 0 x 16 + 1 x 32 = 45

Therefore, the decimal equivalent of the binary number 101101 is 45.

We can write a JavaScript function to convert binary to a decimal using the above method. Here is an example:

function binaryToDecimal(binary) {
let decimal = 0;
let power = 0;
while(binary !== 0) {
const digit = binary % 10;
decimal += digit * Math.pow(2, power);
binary = Math.floor(binary / 10);
power++;
}
return decimal;
}

// example usage
console.log(binaryToDecimal(101101)); // output: 45

In the above code, we define a function called binaryToDecimal that takes a binary number as input and returns its decimal equivalent. We initialize the decimal variable to 0 and the power variable to 0. We use a while loop to iterate over each binary digit. Inside the loop, we extract the rightmost digit of the binary number using the modulo operator (%) and calculate its decimal equivalent using the

How to Convert Binary to Decimal Javascript
Scroll to top