HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JS Decimal to Binary Converter</h1> <script> var num = 500; var arr = []; var i = -1; var j; while(num != 0) { i++; arr[i] = num % 2; console.log(arr[i]); num = Math.floor(num / 2); } //reverse the array and display for(j = i; j >= 0; j--) document.write(arr[j]); </script> <p><strong>Note</strong>: In this example, the decimal number (500) is converted into the binary number (111110100).</p> </body> </html>
OUTPUT
×

Save as Private