Check if value is number Javascript & ReactJs – Code Example

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