JavaScript

How to convert a string into an integer using the parseInt() function in JavaScript?

JavaScript has the ability to allow the programmer to create whatever they want. JavaScript allows you to write dynamic code to render the live changes dynamically. Converting a string is very common when you are developing an application, you have to deal with the different formats of the variable and therefore you need to handle every format using a specific method. This tutorial is going to help you to figure out how you can easily convert a string into a number.

Approaches to convert a string into a number

There are many approaches to convert a string into a number. In JavaScript, there are two methods that are being used to convert a string into a number:

  • The first method is to parse a string into a number,
  • While the other method is to convert the type of the string into a Number.

In this article, we are going to learn how we can easily convert a string into a number with the JavaScript parseInt() method.

JavaScript parseInt() method

This method returns the number after converting a string into an integer,. Let’s look at it practically that how the parseInt() method works and how we can use it to convert a specific numeric formatted string into a number.

let number = parseInt("25");
console.log("The type is: ",typeof(number), "and the value is: ", number);

Output

As you can see, we have passed a string but the type of the string has been changed to a number and the value can now be used for any numeric operation.

ParseInt() method with multiple arguments

The parseInt() method accepts many strings as its arguments but it only parses the first one into a number while it ignores all the other strings following the first one.

Let’s take a look at the following example:

let number = parseInt("30 50");
console.log("The type is: ",typeof(number), "and the value is: ", number);

Output

As I described above that parseInt() method only parses the first string while it ignores all the other strings following the first one, so here in the above example, you can clearly see that the “30” is parsed into a number while the 50 followed by the 30 is being ignored by the method.

Passing extra spaces to ParseInt() method

The parseInt() method by default ignores all the spaces around the string and targets the strings exactly from where the exact string is being started.

Let’s take a look at this in the following example,

let number = parseInt(" 25 ");
console.log("The type is: ",typeof(number), "and the value is: ", number);

Output

As you can see in the above example, the spaces are being ignored by the parseInt() method and producing the desired result.

Passing alphabets to the parseInt() method

The parseInt() method will return a NaN as a result if you provide a string that is not in the numeric format.

let number = parseInt("number");
console.log("The type is: ",typeof(number), "and the value is: ", number);

Output

As you can see above, when you pass the alphabetic string into the method, it parses its type into the number but because the string was not a numeric formatted string, it produces a result with NaN which means the provided format was not accepted by the method.

Passing a float number to the parseInt() method

If you input a floating format string, just the first integer portion will be returned.

let number = parseInt("25.99");
console.log("The type is: ",typeof(number), "and the value is: ", number);

Output

As you can see in the above output, the string after the floating point is ignored by the method and only the string before the floating point is parsed into the number.

Passing comma separated multiple values to the parseInt() method

When you type a comma-separated text, just the first number is parsed and returned.

let number = parseInt("25,99");
console.log("The type is: ",typeof(number), "and the value is: ", number);

Output

JavaScript radix method

The parseInt() method is not limited to converting the strings into the integer only but it can also be used to convert the strings into the desired base as well. All you need to do is just to provide the base when passing the string and that’s it for the method, the method is now responsible for converting that specific string into the provided base.

The decimal number system typically employed by humans has a default value of ten. This parameter (10) should always be included to avoid reader misunderstanding and to ensure predictable behavior.
The JavaScript radix method is also the same, which implies that it accepts the first argument as the string that has to be parsed into a number while the second argument is the base to which we want the method to convert the string to.

Let’s take a look at the default base of the method first.

Radix 10 – (Default)

let number = parseInt("35”, 10);
console.log("
The type is: ",typeof(number), "and the value is: ", number);

The above could return 35 which actually is defined as 3 x 10 + 5 = 35.

Radix – 8
Let’s take another example, in which we want to parse the string into the octal numbers. We will pass 8 as the base number as shown in the code given below:

let number = parseInt("56",8);
console.log("The type is: ",typeof(number), "and the value is: ", number);

The above code returns 46 which is actually calculated as

= 5 x (8)¹ + 6 x (8)
= 5 x 8 + 6 x 1
= 40 + 6
= 46

Output

Radix – 16

Let’s take another example with base-16 this time.

let number = parseInt("56",16);
console.log(number)

Output

Conclusion:

JavaScript uses many approaches to convert a numeric formatted string into a number, some of the major approaches that we have discussed in this article is the ParseInt() method which is used to convert the specific numeric formatted string into a number.

The parseInt() method is not only restricted to converting the string into a number but this method is also capable of converting the string with the specific base as well. We have seen a couple of examples of both types where we converted the simple string into the integer and then we have looked at how we can specify the base with the string that we wanted to be converted to an integer format.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.