Examples
- JavaScript Program to Check if a Number is Positive, Negative, or Zero
- JavaScript Program to Find the Largest Among Three Numbers
- JavaScript Program to Check Prime Number (4 Ways)
- JavaScript Program to Find the Factorial of a Number
- Armstrong Number in JavaScript (6 Programs)
- JavaScript Program to Find HCF or GCD
- JavaScript Program to Find LCM (5 Ways)
- JavaScript Program to Convert Decimal to Binary
JavaScript Program to Convert Decimal to Binary
Binary numbers are represented in base-2, consisting of only two digits: 0 and 1. Converting a decimal (base-10) number to binary is a common programming task, useful in computer science and digital electronics.
For example:
-
The binary representation of 10 is 1010.
-
The binary representation of 15 is 1111.
This tutorial will demonstrate how to convert decimal to binary in JavaScript using different approaches. Whether you are looking for a JavaScript program to convert decimal to binary or exploring how to work with decimal to binary JavaScript code, this guide has all covered.
JavaScript Programs to Convert Decimal to Binary Using Built-in Methods
JavaScript provides a built-in method, toString(2), to convert a number to binary.
Code
function convertToBinaryUsingBuiltIn(decimal) {
return decimal.toString(2);
}
// Test the function
const decimalNumber = 10;
console.log(`The binary representation of ${decimalNumber} is:`, convertToBinaryUsingBuiltIn(decimalNumber));
Output
The binary representation of 10 is: 1010
Explanation
-
The toString(2) method converts the number to its binary equivalent.
-
This method is ideal for quick solutions to convert decimal to binary in JavaScript.
Convert Decimal to Binary in JavaScript Using Manual Calculation (Division by 2)
This method involves dividing the number by 2 repeatedly and recording the remainders.
Code
function convertToBinaryManually(decimal) {
let binary = "";
let num = decimal;
while (num > 0) {
binary = (num % 2) + binary;
num = Math.floor(num / 2);
}
return binary || "0";
}
// Test the function
console.log(`The binary representation of ${decimalNumber} is:`, convertToBinaryManually(decimalNumber));
Output
The binary representation of 10 is: 1010
Explanation
-
Divide the number by 2, record the remainder, and update the number as the quotient.
-
This is an alternative to decimal to binary js code for those interested in manual logic.
Convert Decimal to Binary in JavaScript Using Recursion
This program uses a recursive function to calculate the binary equivalent.
Code
function convertToBinaryUsingRecursion(decimal) {
if (decimal === 0) {
return "0";
}
if (decimal === 1) {
return "1";
}
return convertToBinaryUsingRecursion(Math.floor(decimal / 2)) + (decimal % 2);
}
// Test the function
console.log(`The binary representation of ${decimalNumber} is:`, convertToBinaryUsingRecursion(decimalNumber));
Output
The binary representation of 10 is: 1010
Explanation
-
The base cases handle when the number is 0 or 1.
-
The function calls itself with the quotient of the number divided by 2 and appends the remainder.
Convert Decimal to Binary in JavaScript Using Array and Math Methods
This method converts the decimal number to binary using Array and Math methods.
Code
function convertToBinaryUsingArray(decimal) {
const binaryArray = [];
let num = decimal;
while (num > 0) {
binaryArray.unshift(num % 2); // Add remainder at the beginning
num = Math.floor(num / 2);
}
return binaryArray.length ? binaryArray.join("") : "0";
}
// Test the function
console.log(`The binary representation of ${decimalNumber} is:`, convertToBinaryUsingArray(decimalNumber));
Output
The binary representation of 10 is: 1010
Explanation
-
The Array.unshift method adds the remainder to the start of the array.
-
The join method combines all elements into a binary string.
Concepts Used in Above Programs
A. Built-in Methods
-
JavaScript’s toString(2) simplifies binary conversion in one step.
B. Division by 2
-
Repeatedly dividing the number by 2 and collecting the remainders builds the binary representation manually.
C. Recursion
-
Recursive calls break down the problem into smaller parts and build the binary value step by step.
D. Array and Math Methods
-
Arrays and methods like unshift and join offer an alternative way to collect and format the binary representation.