Skip to main content
X

Explore your training options in 10 minutes

JavaScript ParseInt: A Step-By-Step Guide

James Gallagher - December 29, 2020


The JavaScript parseInt method reads a string or a float and converts it to an integer. This method is commonly used to prepare a string for a mathematical operation because strings cannot be treated like numbers in a math equation.


When you’re working with data in JavaScript, you may want to convert a string into an integer. For example, say you want to ask a user to insert their age in a web form. You may want to convert the age they have entered into a number. This will let you check if the user is over the age of sixteen.

That’s where the JavaScript parseInt() function comes in. ParseInt() is a built-in method that can be used to convert a string into an integer.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

In this tutorial, we are going to explore the basics of the JavaScript parseInt() method. We’ll discuss the syntax used with the parseInt() method. We’ll walk through an example to showcase how you can convert a string to a number in JavaScript.

JavaScript parseInt()

JavaScript parseInt() converts a string or a floating-point number to an integer. It is commonly used to prepare data formatted as a string for a mathematical operation.

Here’s the syntax for the JavaScript parseInt() function:

parseInt(string, radix);

The parseInt() method takes two parameters. The first one is the string you want to convert to an integer and is required.

The second parameter, called the “radix,” is optional. It represents the radix in which your value appears that is required for your conversion.

The radix argument is not as commonly used as the last one. In most cases, your numbers will be formatted in 10 decimal. The default radix is 10 decimal so you do not need to worry about this argument.

You can include spaces at the start and end of a value that you want to convert to a number. But, spaces in the middle of a number are not allowed.

If you try to parse a value that includes letters, you will receive a NaN value. The radix argument may let you include some letters, such as “x” in your number. This is only the case where the letter is relevant to how a number is formatted.

parseInt() JavaScript Example

Let’s use an example to illustrate how this works. Let’s say that we have a string “20202” that we want to convert to an integer. We could do so using the following code:

console.log(parseInt("20202"));

The parseInt() function parses a string and returns an integer as follows:

20202

Let’s check the JavaScript console to see the result of our code. Our program has returned a number instead of a string. We can tell our value is now a number because there are no quotation marks in the value.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

We assign a value to a JavaScript variable called “value”. Our value is a string, which is indicated by the quotation marks surrounding the value. Then, we use the parseInt() method to convert that value to an integer.

Similarly, if we use the “radix” argument, we can convert our numbers to strings in different bases.

The “radix” argument takes in a number between 2 and 36 and describes the numerical system the parseInt() function should use. Say we want to convert a “15” from hexadecimal that has a radix value of “16.” We could do so using the following code:

console.log(parseInt('0xF', 16));

Our function returns: 15.

Passing Invalid Values to parseInt()

In addition, if we pass a string that cannot be converted into a number, JavaScript will return “NaN,” which stands for Not a Number. Here’s an example of using parseInt() to convert “Hey” into a string:

console.log(parseInt('Hey'));

Our number parseInt returns “NaN,” because the “Hey” string characters cannot be converted into a number.

Conclusion

The JavaScript parseInt() method can be used to convert a string to an integer. This function is useful if you have a string of data that needs to be formatted as a number. For example, you may want a string to be a number so you can perform mathematical operations on that string.

In this tutorial, we explored how to use the JavaScript parseInt() method and discussed a few examples of the method in action. We also analyzed how the “radix” parameter can be used with parseInt() .

Now you have the information you need to start using the JavaScript parseInt() method like a pro! If you’re looking for more resources to help you learn JavaScript, check out our complete How to Learn JavaScript guide .

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.

Skip to main content