Open In App

JavaScript Program to Convert Decimal to Binary

In this article, we are going to learn the conversion of numeric values from decimal to binary. Binary is a number system with 2 digits (0 and 1) representing all numeric values.

Given a number N which is in decimal representation. our task is to convert the decimal representation of the number to its equivalent binary representation.



Example 1:

Input : 7
Output : 111

Input : 10
Output : 1010

If the decimal number is 10.



There are several approaches that can be used to Convert Decimal to Binary, which are listed below:

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using unsigned right shift operator (>>>)

The easiest and most basic way to convert decimal to binary is to use the unsigned right shift operator for converting the input number to an unsigned 32 bit integer. We make use of the toString method with argument 2 to specify that the output should be in base 2 i.e. binary.

Syntax:

function decimalToBinary(N) {
return (N >>> 0).toString(2);
}

Example: In this example, we are using the above-explained approach.




function decimalToBinary(N) {
    return (N >>> 0).toString(2);
}
  
let N = 10;
let binary = decimalToBinary(N);
console.log(
    "The binary representation of given number is:- " + binary);

Output
The binary representation of given number is:- 1010

Approach 2: Using Loop and String Concatenation

In this approach, we use loop and string concatenation

Syntax:

while (N > 0) {
binary = (N % 2) + binary;
N = Math.floor(N / 2);
}

Example: In this example, we are using the above explained approach.




function decimalToBinary(N) {
    let binary = '';
  
    while (N > 0) {
        binary = (N % 2) + binary;
        N = Math.floor(N / 2);
    }
  
    return binary;
}
  
let N = 10;
let binary = decimalToBinary(N);
console.log(
    "The binary representation of given number is:- " + binary);

Output
The binary representation of given number is:- 1010

Approach 3: Using toString(2) Method

The toString(2) method is a built-in JavaScript method available for number objects that converts a numeric value (decimal number) to its binary representation as a string.

Syntax:

num.toString(2)

Example: In this example, we are using the toString(2) method to convert our given numeric value to binary.




function decimalToBinary(val) {
    return val.toString(2);
}
  
let num1 = 10;
let result = decimalToBinary(num1);
console.log("Binary representation:", result);

Output
Binary representation: 1010

Approach 4: Using Array and Math method

This approach converts a decimal number to binary using an array to store binary digits. It repeatedly divides numbers by 2 and adds remainders to the beginning of the array. The binary representation is then obtained by joining the array elements.

Syntax:

let arr = [];
for (; num1 > 0; num1 = Math.floor(num1 / 2)) {
arr.unshift(num1 % 2);
}
return arr.join("");

Example: In this example, we are using the above-explained approach.




function decimalToBinary(num1) {
    if (num1 === 0) return "0";
  
    let arr = [];
    for (; num1 > 0; num1 = Math.floor(num1 / 2)) {
        arr.unshift(num1 % 2);
    }
    return arr.join("");
}
  
let givenNumber = 10;
let result = decimalToBinary(givenNumber);
console.log("The binary representation is: " + result);

Output
The binary representation is: 1010

Article Tags :