
What is the Purpose of the Second Param in parseInt?
It is often overlooked or an unknown part ofย parseInt
, but we have a second parameter that is responsible for the radix: The numeral system to be used. It can be an integer between 2 and 36. It is always a good practice to define the radix when usingย parseInt
. Take the following as an example:
// This will return NaN
parseInt(2, 2);
// This will return 2
parseInt(2, 10);
Copied to clipboard!
If you simply want to convert a string into a number, you can also use the Number
function more reliably, which doesn't require a radix:
Number('2'); // returns 2
Number(2); // returns 2
Number('a'); // returns NaN
Copied to clipboard!
When the function is used on an invalid number, it will return NaN
.

Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.

Resources:
๐ More Webtips
Master the Art of Frontend
Unlimited access to hundreds of tutorials
Access to exclusive interactive lessons
Remove ads to learn without distractions