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

<script>
  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>");
</script>
refresh

Example – isNaN returning true

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

Comments

Click here to write/view comments