JavaScript Program to Convert Decimal to Binary Number
When it comes to creating applications for the web, JavaScript is a popular choice. Converting from decimal to binary is a common task in computer science, and we'll go over it here. The base-2 number system used by computers and other digital devices requires that decimal numbers be converted to binary.
There are many algorithms that can perform this transformation, and we'll look at one that uses JavaScript in this article. By the end of this guide, you will be fluent in using JavaScript to perform the binary-to-decimal conversion. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.
What is a Binary Number?
Simply put, a binary number is a number written in the base-2 numeral system. Only the numbers 0 and 1 are used in the binary system. A "bit" is a binary digit, and there is one for each digit in a binary number. Alternatively, the decimal system (base 10) employs the digits 0 through 9.
Since on/off states of transistors in a computer are sufficient to represent binary numbers, they find widespread application in computing and digital electronics. In computer memory, for instance, each bit can store either a 0 (indicating "off") or a 1 ("on") value. One of the most fundamental aspects of how computers work is the use of the binary system for encoding and decoding data.
For example, let x =36 and creates a new variable x that contains the base 10 number 36.
We basically have two ways to convert the Decimal to Binary, one is by conventional method and the other is using toString() method.
Method 1 : Program to Convert Decimal to Binary Convert Decimal to Binary
The number entered by the user is passed as an argument to the convertToBinary()
function. The while loop is used until the number entered by the user becomes 0. Here, rem is the modulus %
value of the number when divided by 2 and i gives the place value of the binary number.
// JavaScript Program to convert decimal to binary
function convertToBinary(x) {
let bi = 0;
let rem, i = 1, step = 1;
while (x != 0) {
rem = x % 2;
// console.log(`Step ${step++}: ${x}/2, Remainder = ${rem}, Quotient ${parseInt(x/2)}`);
x = parseInt(x / 2);
bi = bi + rem * i;
i = i * 10;
}
console.log(`Binary: ${bi}`);
}
const num = prompt('Enter a decimal number: ');
convertToBinary(num);
Enter a decimal number : 36
Binary: 100100
Method 2 : Program to Convert Decimal to Binary Convert Decimal to Binary Using toString() Method
The JavaScript built-in method toString([radix])
returns a string value in a specified radix (base). Here, toString(2)
converts the decimal number to a binary number.
// JavaScript Program to convert decimal to binary
const num = parseInt(prompt('Enter a decimal number: '));
const result = num.toString(2);
console.log('Binary:' + ' ' + result);
Enter a decimal number: 9
Binary: 1001
Conclusion
Finally, converting from decimal to binary is a common task that can be accomplished with little effort in JavaScript. A firm grasp of the tenets of the binary number system and the algorithms covered in this tutorial allows you to perform this transformation with ease in your own code.