What is the Purpose of the Second Param in parseInt?

What is the Purpose of the Second Param in parseInt?

Ferenc Almasi โ€ข 2021 January 08 โ€ข ๐Ÿ“– 1 min read
  • twitter
  • facebook
๐Ÿ“’ JavaScript

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.

How does the second parameter of parseInt do in JavaScript?
If you would like to see more Webtips, follow @flowforfrank

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


Did you find this page helpful?
๐Ÿ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundreds of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro