How to convert a Decimal number to Binary in JavaScript

A Decimal is a term that describes the base-10 number system.

  • For example: (47)10(47)_{10}

A Binary is a term that describes the base-2 number system.

  • For example: (1101)2(1101)_{2}

Decimal to Binary conversion

Step 1: Divide the decimal number by 2 and write down the remainder value.

Step 2: Divide the quotient by 16 and write the remainder again.

Step 3: Repeat steps 1 and 2, until you get the quotient as 0.

Step 4: Now, write the remainders in reverse order, starting with the last remainder.

Step 5: Another way to read this is that the binary number’s Least Significant Bit (LSB) is at the top and its Most Significant Bit (MSB) is at the bottom.

Below is a visualization to help you understand the above steps to convert decimal to binary.

Example: Covert decimal number 131013_{10} into binary.

1 of 4

Example

The following code shows the conversion from decimal to binary number.

let n = 13;
console.log(n.toString(2));

Line 1: Declaring a variable n.

Line 2: The console.log() method writes a message to the console, and n.toString(2) converts the number to the string using base 2.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved