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

  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>");

Example – parseInt returning NaN

  document.write(parseInt("abc") + "<br>");
  document.write(parseInt("") + "<br>");
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments