Write a JavaScript function to convert Decimal number to Binary number.
Check out all 100 + JavaScript Examples
Check out all JavaScript programs at All JavaScript Programs
JavaScript Convert Decimal to Binary
function decimalToBinary (num) {
var bin = []
while (num > 0) {
bin.unshift(num % 2)
num >>= 1 // basically /= 2 without remainder if any
}
console.log('The decimal in binary is ' + bin.join(''))
}
decimalToBinary(2)
decimalToBinary(7)
decimalToBinary(35)
Output
The decimal in binary is 10
The decimal in binary is 111
The decimal in binary is 100011
Comments
Post a Comment