Home Programs How to Convert Decimal to Binary in JavaScript

How to Convert Decimal to Binary in JavaScript

In this article we show a simple way to convert a decimal number to a binary number using JavaScript

Example 1

The user is prompted to enter a number.

The parseInt() method is used to convert a string value to an integer and we store this as a constant

We then use the toString method which allows you to specify a number base

// convert decimal to binary

// take input
const number = parseInt(prompt('Enter a decimal number: '));

// convert to binary
const result = number.toString(2);

console.log('Binary:' + ' ' + result);

We then input the number 15 like this

When you run this you will see something like this

Binary: 1111

You may also like