how to convert int to bynary in javascript? code example

Example 1: how to get binary value of int javascript

const convertNumberToBinary = number => {
    return (number >>> 0).toString(2);
}

Example 2: 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;
}

Tags:

Java Example