Convert binary number into decimal in javascript & Reactjs – Code Example

Total
0
Shares

To convert a binary number into decimal in javascript and reactjs, you can use parseInt() function with base 2. It can handle automatically if the number is provided in the form of string. Check this code example –

function binaryToDecimal(x) {
  const parsed = parseInt(x, 2);
  if (isNaN(parsed)) { return 0; }
  return parsed;
}

console.log(binaryToDecimal('1001'));
// expected output: 9

Source: MSDN