[Code Example] Handle divide by 0 properly in Javascript & ReactJs

Total
0
Shares

To handle divide by 0 properly in Javascript & ReactJs, you can use isFinite() function which returns true if the number is finite. Check this code –

function div(x) {
  if (isFinite(1000 / x)) {
    return 'Number is NOT Infinity.';
  }
  return 'Number is Infinity!';
}

console.log(div(0));
// expected output: "Number is Infinity!""

console.log(div(1));
// expected output: "Number is NOT Infinity."

Source: MSDN

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like