Javascript – parseInt

The parseInt() function parses a string argument and returns an integer of the specified radix (default 10). In case the string starts with a valid number and ends with non numeric characters, parseInt still returns the parsed Integer by parsing upto the first non-digit.

parseInt(string, radix);

Example – parseInt returning valid number

<script>
  document.write(parseInt("37") + "<br>");
  document.write(parseInt("-37") + "<br>");
  document.write(parseInt("+37") + "<br>");
  document.write(parseInt(" 37") + "<br>");
  document.write(parseInt("37abc") + "<br>");
  document.write(parseInt("37.37") + "<br>");
</script>
refresh

Example – parseInt returning NaN

<script>
  document.write(parseInt("abc") + "<br>");
  document.write(parseInt("") + "<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