JavaScript Program to Convert Decimal to Binary (Using Bitwise Operator, Loop, Recursion, and More)
Introduction
Binary numbers are essential in computing and electronics. JavaScript, though often used for web development, can handle number base conversions efficiently.
This article explores how to perform decimal to binary conversion in JavaScript using various methods — ranging from bitwise operations and loops to recursion and built-in utilities.
Problem Statement
Write multiple JavaScript programs to convert decimal numbers into binary using various techniques like bitwise operators, while loops, recursion, custom functions, and more.
Use Cases
-
Converting numbers for digital electronics simulation.
-
Teaching binary arithmetic in education tools.
-
Building coding interview practice tools.
-
Creating custom binary-to-decimal converters in a web app.
Method 1: Using Bitwise Operator
// Program 1: Convert Decimal to Binary using Bitwise Operator
function convertUsingBitwise(num) {
let binary = "";
let started = false;
for (let i = 31; i >= 0; i--) {
let bit = (num >> i) & 1;
if (bit === 1) started = true;
if (started) binary += bit.toString();
}
return binary || "0";
}
console.log("Binary (25):", convertUsingBitwise(25));
Output
Binary (25): 11001
Explanation
-
Uses bitwise shifting and masking.
-
Omits leading zeros using a
started
flag.
Method 2: Using Recursion
// Program 2: Convert Decimal to Binary using Recursion
function convertRecursive(num) {
if (num === 0) return "";
return convertRecursive(Math.floor(num / 2)) + (num % 2).toString();
}
let binary = convertRecursive(14);
console.log("Binary (14):", binary || "0");
Output
Binary (14): 1110
Explanation
-
Divides the number recursively and appends binary bits during return.
-
Edge case: prints
"0"
if number is 0.
Method 3: Using While Loop and Array
// Program 3: Convert Decimal to Binary using While Loop and Array
function convertUsingArray(num) {
if (num === 0) return "0";
let result = [];
while (num > 0) {
result.push(num % 2);
num = Math.floor(num / 2);
}
return result.reverse().join("");
}
console.log("Binary (37):", convertUsingArray(37));
Output
Binary (37): 100101
Explanation
-
Stores remainders in an array.
-
Reverses and joins to form binary string.
Method 4: Using Built-in toString(2)
// Program 4: Convert Decimal to Binary using toString(2)
function convertUsingToString(num) {
return num.toString(2);
}
console.log("Binary (100):", convertUsingToString(100));
Output
Binary (100): 1100100
Explanation
-
JavaScript has native
.toString(2)
for binary conversion. -
Easiest and most reliable method.
Method 5: Using Left Shift and Mask
// Program 5: Convert Decimal to Binary using Left Shift
function convertLeftShift(num) {
let binary = "";
let bitLength = Math.floor(Math.log2(num)) + 1;
for (let i = bitLength - 1; i >= 0; i--) {
binary += ((num >> i) & 1).toString();
}
return binary;
}
console.log("Binary (19):", convertLeftShift(19));
Output
Binary (19): 10011
Explanation
-
Shifts from the most significant bit and masks to check bit.
-
Works well for fixed-bit binary.
Method 6: Using Stack Simulation
// Program 6: Convert Decimal to Binary using Stack Simulation
function convertUsingStack(num) {
let stack = [];
while (num > 0) {
stack.push(num % 2);
num = Math.floor(num / 2);
}
return stack.reverse().join("") || "0";
}
console.log("Binary (7):", convertUsingStack(7));
Output
Binary (7): 111
Explanation
-
Acts like a stack to reverse bits.
-
Similar to array method but emphasizes LIFO behavior.
Summary
In this article, we covered 6 different techniques to perform decimal to binary conversion in JavaScript. From bitwise manipulation and stack simulations to recursion and built-in methods, JavaScript provides multiple paths to handle binary operations efficiently.