numbers to binary js code example

Example 1: number to binary javascript

// method one
function convertNumberToBinary (num) {
 return parseInt([...num.toString(2)].reverse().join(''), 2);
}

// methode two
function convertNumberToBinary (num) {
 return parseInt(num.toString(2).slice('').reverse().join(''), 2);
}

// methode three
function convertNumberToBinary (num) {
  let binary = (n >>> 0).toString(2).split('').sort().join('');
  let number = parseInt(binary, 2);
  return num;
}

Example 2: how to convert to binary in javascript

function bin(num) {
    var binn = [];
    var c;
    while (num != 1) {
        c = Math.floor(num / 2);
        binn.unshift(num % 2);
        num = c;

    }
    binn.unshift(1)
    return binn
}
//returns list of binary nos. in order