Hello everyone, I’d like to ask for a clarification about radix as second argument to parseInt().
let y = parseInt('11', 2) //=> 3
If I’m not wrong, the radix specifies the base of the number in the string, and can be an integer between 2 and 36. What does that mean? Has something with how a number is written, namely as a decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2)? If yes, what is the difference between these bases and how can that be useful? If not, please provide further explanation.
Thanks for your time and happy coding to everyone!
You’re correct about the radix. It’s being used to determine the base of the string you are parsing. Like in your example the string ‘11’ gets parsed to 3 when radix is 2 (binary). On a base 10 it’s parsed to 11.
The purpose of parsing is to convert your input to a useful output. So parseInt() gives you the option to specify what base your input is. This so you can convert different bases of numbers using the same function.
Instead of having a multitude of functions like parseHexadecimal(), parseBinary() … you can use parseInt() for all by just switching the radix.