Javascript parseInt Function

Updated on 19 Mar 2012,
Published on 07 Jun 2008

Javascript parseInt function converts the string type value into integer. If floating point number is passed to the parseInt function then it returns the integer value truncating precision point values. We discussed about Javascript parseFloat function in the previous article, the javascript parseInt function also follows the same rules that parseFloat function uses. parseInt function reads the first character of string passes if it is a numeric value then it parses the value into integer. If string starts with string character then parseInt function becomes unable to parse the string value and returns NaN.

JavaScript Functions Examples:

You can see the live samples and examples of JavaScript Functions from the following links:

Only leading space character is allowed in the string value passed to the parseInt function. Otherwise parseInt function returns NaN i.e. Not a Number.

Javascript parseInt Function Syntax

parseInt(string);

parseInt function takes string type value as argument that is converted into number or integer type. 

Javascript parseInt Function Examples

document.write(parseInt("123") + "<br />");

document.write(parseInt(" 123") + "<br />");

document.write(parseInt(" 123 ") + "<br />");

document.write(parseInt("123.22") + "<br />");

document.write(parseInt("123a") + "<br />");

document.write(parseInt("123 a") + "<br />");

document.write(parseInt("123 456 789") + "<br />");

document.write(parseInt("number 123") + "<br />");

Note: In above examples parseInt function returned 123 for the input string value "123.22". parseInt function truncates the floating point precision values. Another thing Javascript parseInt function allows leading and trailing space characters but return NaN for the string value not starting with number. E.g. in above code parseInt function returned NaN for "number 123".

Output:

You can see the output of above discussed code from the following link:

JavaScript ParseInt

Continue to next tutorial: Javascript parseInt using Radix to learn how to convert the decimal numbers into other number system.

0 Responses to "Javascript parseInt Function"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers