JavaScript Number parseInt() method
The JavaScript number parseInt() method parses a string argument and converts it into an integer value. With string argument, we can also provide radix argument to specify the type of numeral system to be used.
Syntax
The parseInt() method is represented by the following syntax:
Parameter
string – It represents the string to be parsed.
radix – It is optional. An integer between 2 and 36 that represents the numeral system to be used.
Return
An integer number. It returns NaN, if the first character cannot be converted to a number.
JavaScript Number parseInt() method example
Here, we will understand parseInt() method through various examples.
Example 1
Let’s see a simple example of parseInt() method.
Output:
50 50 NaN 50 50
Example 2
Let’s see an example to add two strings with and without using parseInt() method.
Output:
Before invoking parseInt(): 1020 After invoking parseInt(): 30
Example 3
In this example, we will pass radix argument within parseInt() method.
Output:
50 40 80