Using the JavaScript ParseInt Method

Follow us on LinkedIn for our latest data and tips!

JavaScript’s parseInt function is all about converting a string to an integer. The function takes a string value as an argument and converts it to a numerical value with no decimal places, or alternatively the value NaN. ParseInt has a total of two parameters and thus takes two arguments. The second parameter specifies a radix value (i.e., a value representing the mathematical base that will be used when converting the string value to a number).

A simple example using the conventional base-10 (decimal) counting system looks like this:

var x = "1";

console.log(x, typeof x); //1 string

x = parseInt(x, 10);

console.log(x, typeof x); //1 number

Live code example:

Explore JavaScript Courses

If no argument is passed to parseInt for the radix value, the default value of base-10 will likely be employed. Alternatively, you can 2 for the radix value to tell the parseInt function to use the binary (base-2) numbering system; a value of 8 will tell parseInt to use the octal numbering system; and a value of 16 will tell the parseInt function to use the hexadecimal, or base-16, numbering system. With bases greater than 10, letters of the alphabet are used to represent numbers greater than 9. Wikipedia gives more information about numbering systems; including the binary, octal, decimal, and hexadecimal systems, which have their own entries:

To show an example of using a non-decimal system, let’s look at an example of converting a binary number in to an integer in the base-10 system:

var x = 10;

x = parseInt(x, 2);

console.log(x); //2

Live code example:

See that? The variable x was given a value of 10. In binary, a value of 10 (i.e., “one zero”) is equivalent to 2 in the decimal system. We passed 10 to the parseInt function and specified a radix of base-2. The function returned the integer value 2, which is the integer equivalent of “one zero.” Pretty nifty!

So what happens if we pass a numerical value with a decimal point to the parseInt function? Let’s take a look:

var x = 3.14;

x = parseInt(x, 10);

console.log(x); //3

Live code example:

Would you look at that? The parseInt function took our numerical value with a decimal place and parsed it in to an integer. It doesn’t get more titular than that!

But… what if we pass a non-numerical value to parseInt? What happens then? Here’s an example:

var x = "Hola World!";

x = parseInt(x, 10);

console.log(x); //NaN ...or, Not a Number

Live code example:

That’s right, if parseInt is passed an argument that doesn’t readily convert to a numerical value, then the returned value will be that of NaN (or Not a Number). If the function is passed a combination of numerical and alphabetical string values, then the function will convert the numerical characters to a integer and discard the remaining alpha characters. Here’s that in action:

var x = "123UnoDosTres";

Explore JavaScript Courses

x = parseInt(x, 10); console.log(x); //123

Live code example:

So there you have it; the basics of JavaScript’s parseInt function. Remember that parseInt’s primary purpose is to take a string value and convert that value to an integer (no decimal places) of the type number. You can specify the base (or counting system) that you want to use. If the function is passed a value that cannot be readily converted in to a number, then parseInt will return a value of NaN.

Additional information on JavaScript’s parseInt function can be found here: