Javascript – check if string is number

The isNaN() function determines whether a value is NaN (Not-A-Number) or not. Note that parseInt approach may not work with strings having valid number prefix. Alternatively we can also use Number() function.

Example – isNaN returning false

  document.write(isNaN("37") + "<br>");
  document.write(isNaN("37.37") + "<br>");
  document.write(isNaN("") + "<br>");
  document.write(isNaN(" ") + "<br>");
  document.write(isNaN(null) + "<br>");
  document.write(isNaN(true) + "<br>");
  document.write(isNaN(false) + "<br>");

Example – isNaN returning true

  document.write(isNaN("123ABC") + "<br>");
  document.write(isNaN(NaN) + "<br>");
  document.write(isNaN(undefind) + "<br>");
  document.write(isNaN({}) + "<br>");
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments