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.
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.
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.