[Code Example] Check if value is number Javascript & ReactJs

Total
0
Shares

To check if the value is a number in Javascript & ReactJs, you can use isNaN() function. Check the code –

function sanitise(x) {
  if (isNaN(x)) {
    return NaN;
  }
  return x;
}

console.log(sanitise('1'));
// expected output: "1"

console.log(sanitise('NotANumber'));
// expected output: NaN

Source: MSDN

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like