To convert an integer to binary in JavaScript, divide the integer by 2 and store the remainder.
Example
Following is the code −
function convertDecimalToBinary(value) {
var binaryValues = [];
var counter = 0;
while (value > 0) {
binaryValues[counter++] = parseInt(value % 2);
value = parseInt(value / 2);
}
for (var j = counter - 1; j >= 0; j--)
process.stdout.write(binaryValues[j] + "");
}
convertDecimalToBinary(5);To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo255.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo255.js 101