# How to convert Decimal to Binary in JavaScript?

HTMLJavascriptProgramming Scripts

#### Kickstart HTML, CSS and PHP: Build a Responsive Website

Featured

59 Lectures 8.5 hours

#### Web Design for Beginners: Build Websites in HTML & CSS 2022

68 Lectures 8 hours

#### Complete HTML/CSS Course 2022

Featured

47 Lectures 12.5 hours

This tutorial will teach us to convert the decimal number to a binary number string. The binary number is the string of only 0 and 1. The computer doesn’t understand the high-level programming language, so we need to instruct the computer in a low-level language, represented as the binary string.

Also, binary string is used in digital electronics. Here, we have three methods to convert the decimal number to a binary string.

• Using the toString() Method

• Using the right shift Operation

• Using the modulo Operator

## Using the toString() Method

The toString() method is JavaScript built-in string method that we can use to convert any variable into a string. Also, we can use it to convert the number to a string. If we don’t pass any parameter to the toString() method, it converts a number to a decimal string with base 10. If we pass a different base, it converts a number to the string with a defined base.

### Syntax

Users can follow the below syntax to use the toString() method to convert decimal to binary.

let decimal = 10;

// to convert positive decimal to binary
let binary = decimal.toString( redix );

// to convert negative decimal to binary
Let binary = (decimal >>> 0).toString( redix );

In the above syntax, to convert the negative decimal number to a binary string, users can see we need to shift the number by 0 bit. The >>> shifts the signed number by a fake bit to convert it to its corresponding positive number. When we apply a fake bit shit operation, the bit of singed number remains the same, but it just converts the number to positive.

If the user performs the decimal to binary on negative numbers without using the fake bit shift operation, the toString() method appends only the ‘-’ sign before the integer. It doesn’t return the actual binary string of the negative number.

### Parameters

• redix − It is the base for that base users want to convert the number to a string. In our case, radix will always be 2 as we need to convert decimal numbers to strings with base 2, meaning binary string.

### Example

<html>
<title>Convert decimal to binary using JavaScript.</title>
<body>
<h2>Convert decimal to binary using JavaScript <i> toString(2) </i> method.</h2>
<h4>Examples of decimal and its binary string.</h4>
<div id = "binary"> </div>
</body>
<script>
var binrayOutput = document.getElementById("binary");
let decimal = 1435;
binrayOutput.innerHTML += "1435 == " + decimal.toString(2) + ". <br/> ";
decimal = -1435;
binrayOutput.innerHTML += "-1435 == " + (decimal >>> 0).toString(2) + ". <br/> "; // shifting the fake bit
decimal = 0;
binrayOutput.innerHTML += "0 == " + decimal.toString(2) + ". <br/> ";
decimal = 9658798;
binrayOutput.innerHTML += "9658798 == " + decimal.toString(2) + ". <br/> ";
</script>
</html>


## Using the right shift Operation

This method will use the right shift operator to convert the decimal number to a binary string. Users can follow the below algorithm and syntax to achieve our goal.

### Syntax

while (decimal > 0) {
if (decimal & 1) {
binary = "1" + binary;
} else {
binary = "0" + binary;
}
decimal = decimal >> 1;
}

### Algorithm

• STEP 1 − Create an empty string and a decimal number.

• STEP 2 − Iterate through the decimal number while it is greater than 0.

• STEP 3 − To get the last bit of the decimal number, perform bitwise & operation of 1 with decimal number. If the last bit is 1, append “1” in the front of the binary string. Otherwise, append ‘0’ in the front of the binary string.

• STEP 4 − Remove the last bit from the decimal number using the right shift operator.

### Example

In the below example, we have implemented the above algorithm to convert decimal number to binary string.

<html>
<title> Convert decimal to binary using JavaScript. </title>
<body>
<h2> Convert decimal to binary using <i> Right shift operator </i> in JavaScript. </h2>
<h4> Converting 756435 to binary string. </h4>
<div id = "binary"> </div>
</body>
<script>
var binrayOutput = document.getElementById("binary");
let decimal = 756435;
let binary = "";
while (decimal > 0) {
if (decimal & 1) {
binary = "1" + binary;
} else {
binary = "0" + binary;
}
decimal = decimal >> 1;
}
binrayOutput.innerHTML = binary;
</script>
</html>


## Using the modulo Operator

We will use the modulo and division operation in this method. We will take the modulo of number with 2 and keep dividing it by 2. If the modulo with the number is 1, we will append “1” in front of the binary string. Otherwise, we will append “0” in front of the binary string.

### Syntax

Users can follow the below syntax to implement this method.

// iterate through the number till it becomes 0.
while (decimal > 0) {
// if modulo of number with 2 is ‘1’, append 1 in front of binary string. Otherwise append 0.
if (decimal % 2 == 1) {
binary = "1" + binary;
} else {
binary = "0" + binary;
}
// divide number by 2.
decimal = Math.floor(decimal / 2);
}

### Example

In the below example, we have implemented the above method to convert decimal to binary using the modulo operator.

<html>
<title> Convert decimal to binary using JavaScript. </title>
<body>
<h2> Convert decimal to binary using <i> Modulo operator </i> in JavaScript. </h2>
<h4> Converting 98 to binary string. </h4>
<div id="binary"></div>
</body>
<script>
var binrayOutput = document.getElementById("binary");
let decimal = 98;
let binary = "";
while (decimal > 0) {
if (decimal % 2 == 1) {
binary = "1" + binary;
} else {
binary = "0" + binary;
}
binrayOutput.innerHTML += "decimal is = " + decimal + " reminder is = " + decimal % 2 + " Quotient is = " + Math.floor(decimal / 2) + ". <br/>";
decimal = Math.floor(decimal / 2);
}
binrayOutput.innerHTML += "Binary string is " + binary;
</script>
</html>


In the above output, we have rendered the output of every step.

The first approach is useful for converting the negative decimal numbers to binary strings. The second and third approach is only useful for the positive decimal numbers. It is recommended to use the first approach as it takes a single line to write the code.

Updated on 10-Aug-2022 11:51:59