Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

JavaScript Program to Converter a Binary to Decimal

Updated on Oct 30, 2024
By Mari Selvan
πŸ‘οΈ 104 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
JavaScript Program to Converter a Binary to Decimal

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the world of programming, understanding different number systems is crucial. One common conversion task is to convert a binary number to its decimal equivalent.

Binary is a base-2 number system, while decimal is a base-10 system.

In this tutorial, we'll explore a javascript program that efficiently converts a binary number to its decimal representation.

πŸ“„ Example

Let's dive into the javascript code that performs the binary to decimal conversion.

binaryToDecimal.js
Copied
Copy To Clipboard
// Function to convert binary to decimal
function binaryToDecimal(binaryNumber) {
  let decimalNumber = 0;
  let binaryString = binaryNumber.toString();
  let binaryLength = binaryString.length;

  for (let i = 0; i < binaryLength; i++) {
    let currentDigit = parseInt(binaryString[i]);
    let power = binaryLength - 1 - i;
    decimalNumber += currentDigit * Math.pow(2, power);
  }

  return decimalNumber;
}

// Driver program
// Replace this binary number with your desired value
const binaryNumber = 101010;

// Call the function to convert binary to decimal
const decimalNumber = binaryToDecimal(binaryNumber);

console.log(`Binary: ${binaryNumber}`);
console.log(`Decimal: ${decimalNumber}`);

πŸ’» Testing the Program

To test the program with a different binary number, replace the value of binaryNumber in the main function.

Output
Binary: 101010
Decimal: 42

Compile and run the program to see the binary-to-decimal conversion in action.

🧠 How the Program Works

  1. The program defines a function binaryToDecimal that takes a binary number as input and returns its decimal equivalent.
  2. Inside the function, it converts the binary number to a string to iterate through each digit.
  3. For each digit, it calculates the decimal value by multiplying the digit by 2 raised to the power of its position.
  4. The console.log statements in the main program test the conversion by providing a binary number and printing both the binary and decimal representations.

🧐 Understanding the Concept of Binary and Decimal Systems

  • Binary System (Base-2): In the binary system, numbers are represented using only two digits, 0 and 1. Each digit's place value is a power of 2.
  • Decimal System (Base-10): The decimal system is the standard number system that we use every day. It uses ten digits, 0 through 9, and each digit's place value is a power of 10.

Binary-to-decimal conversion involves multiplying each binary digit by 2 raised to the power of its position and summing up the results.

For example, the binary number 101010 is converted to decimal as follows:

1 * 2^5 + 0 * 2^4 + 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 0 * 2^0 = 42

🎒 Optimizing the Program

While the provided program is effective, consider exploring optimizations or handling edge cases for larger binary numbers.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (JavaScript Program to Converter a Binary to Decimal), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy