JavaScript

JavaScript parseInt() Function

The parseInt() function in the JavaScript takes up a string containing any numeric characters and returns an integer equivalent to that string. Another feature of the parseInt() function is the option to define the base or “radix” of the number system of the given string. To understand the parseInt() in javascript, observe its syntax.

Syntax of parseInt() method

The syntax of the parseInt() is given as

var1 = parseInt( stringWithInteger, base)

The details of the syntax are defined as:

  • var1: The variable in which javascript will store the output from the parseInt() function
  • stringWithInteger: The string from which the integer value is to be fetched
  • base: The base defining the number system of the values inside the string (default is 10).

Return Type

The return value from the parseInt() function is an integer value or a NaN in case of an invalid argument.

Additional Information

Some key points must be kept in mind when working with the parseInt() function in JavaScript. These are as follows:

  • If the string contains blank spaces on either end of the numeric characters, then these spaces are ignored
  • If the first character inside the string is not a numeric value or a numeric character, then a NaN is returned to the caller
  • If there are multiple values with spaces in between them, then only the first one would be returned
  • If the base value is smaller than 2 or larger than 32, then a NaN is returned
  • If a string starts with “0x” then it would automatically be taken as a hexadecimal value

Example 1: Passing a valid string, without the radix argument

Create a new string value containing a valid input and pass that to the parseInt() method using the following lines:

var str1 = "174";

let resultValue = parseInt(str1);

Then pass the resultValue variable to the console log function:

console.log(resultValue);

After executing the lines mentioned above, the output will look like this:

The result on the terminal shows that the parseInt() method fetched the integer value 174 from the given string.

Example 2: Giving Float values in the string

To test the result of the parseInt() method with some decimal point in the string, create a new string with the following lines

var str1 = "123.456";

Pass this string into the parseInt() and store the result in a new variable

let resultValue = parseInt(str1);

At the end, pass this variable to console log function to display the output on the terminal

console.log(resultValue);

The result on the terminal will look like the following screenshot

After observing the output, it is clear that only the integer part inside the string value is converted. At the point where the parseInt() discovers the first non-integer value, it stops.

Example 3: Defining the base of the string value as binary

To define the radix or the base of the number of the values inside the string, first, create a new string in binary format like

var str1 = "111";

After that, pass this string variable to the parseInt() method and pass the value 2 in the second argument like

let resultValue = parseInt(str1, 2);

Finally, use the console log function to display the result as:

console.log(resultValue);

The terminal will look like this

As the output shows, the value 111 in binary is equivalent to 7 in the decimal number system.

Example 4: Blank spaces on either end of the value

Simply create a new string variable and have leading and trailing spaces around the integer value like

var str1 = " 542 ";

Pass this variable to parseInt(), store the result in a new variable, and then pass that variable to the console log function

let resultValue = parseInt(str1);

console.log(resultValue);

Executing the program gives the following result

It is clear from the screenshot above, that the parseInt() method ignores the blank spaces on either end of the numeric value.

Wrap up

JavaScript’s parseInt() function takes in a string argument and an optional base argument and then fetches the integer value from the string and returns it as the output. The base argument defines the number system of the value contained inside the string, and the string is always converted into a decimal number system. Multiple scenarios can happen in the case of string variations, and all of these have been shown above.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.