What does radix actually means? Why do we need it?
parseInt(10, radixValue);
What does radix actually means? Why do we need it?
parseInt(10, radixValue);
It's just my opinion but idea that "we need to use radix" quickly becomes outdated. The problem really was actual some time ago because people outside IT world usually don't use number notations other than decimal and quite often provide decimal numbers zero-padded like "010". But since ECMAScript 6 octal numbers in JS are prefixed by "0o" and not just "0" as it was in ECMAScript 5 and 3. So if you don't target IE family (that is not rare situation now) you can skip radix safely.
What is radix in paseInt() ?
The parseInt() function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.
The function call looks like (Syntax):
parseInt(string, radix);
Some examples to clarify the concept of radix
Example 1:
var a = parseInt("11", 2);
The radix variable says that "11" is in the binary system, or base 2. Therefore, this example converts the string "11" to an integer 3.
Example 2:
var a = parseInt("10011", 16);
Here the radix tells the parseInt() that 10011 is a hexadecimal number and hence in integer, it gets converted to 65553
Basically, long story short, the radix argument tells the parseInt() that the string passed as 1st parameter is of a particular system (binary, hexadecimal etc) and it needs to be converted into an integer as an end product.
I learned the hard way that you always need to provide radix when working with parseInt in 2011. but in modern browsers, it seems that they "fixed" it.
If you run a function on a string starting with 0, it will parse it with radix 10. But if you run it on a number for whatever reason, then it will still use radix 8, as mentioned in MDN.
console.log(parseInt('015'));
// This returns 15
console.log(parseInt(015));
// This returns 13