What is the second parameter in parseInt?

Deepa Jarout
3 min readJan 8, 2023

--

The JavaScript parseInt method will be discussed in this article. When a string is parsed (broken down), this function returns an integer or NaN. (Not a Number).

Operation of the parseInt function

In order to extract an integer from a string, the parseInt function is typically used. By doing so, the returned value is converted to a real number.

Syntax -parseInt(string)

parseInt() by deepajarout

10 is not a real integer in the example above; it is a string. Due to the fact that we multiply 2 by a string that only has a numerical representation, we get the value 20 when we do so.

By removing 10 from the string and turning it into an actual number, we can use the parseInt function. An illustration would be:

parseInt() by deepajarout

Now that 10 has been removed from the string, the function has changed it to a real integer.

10. From “10” to 10.

Remember how we said the parseInt function may return either an integer or NaN? When would we then obtain a NaN value?

When a string has text prior to a number, this occurs. Because the parseInt method only looks at the first value starting the string, phrases like “deepa” will provide a NaN value. The return value is NaN if the value is not a number.

parseInt() by deepajarout

Redix is a parameter

The redix parameter is a second one that the parseInt method allows. Which number system to use is specified by this argument. The default value is 10 if the redix is missing.
Syntax:

parseInt(string, radix)

The number is typically between 2 and 36. Redix returns NaN if the value is larger than 36 or less than 2, respectively.

The number in the string should be translated from its duodecimal value to its decimal value if we were to supply a redix of 12, which means.

Here is a brief illustration:

deepajarout
parseInt() by deepajarout

Due to the fact that 30’s duodecimal value in base 10 (decimal) is 36, we receive the value 12 back.

Conclusion

We learned how to extract numbers from texts using the parseInt function in this article.

We also learned how the redix argument may be used to specify the number system to be applied when converting the returned integer.

Many thanks for reading!

--

--

Deepa Jarout

I am Software developer.I like problem solving, traveling, photography, dancing, video games, conversation.I like to explore new places with different tradition