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 β€’ Read time 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:

Copied to clipboard! Playground
// This will return NaN
parseInt(2, 2);

// This will return 2
parseInt(2, 10);
parseInt.js

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:

Copied to clipboard!
Number('2'); // returns 2
Number(2);   // returns 2
Number('a'); // returns NaN
Number.js

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

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.