Why do we need to use radix parameter when calling parseInt?
What does radix actually means? Why do we need it?
parseInt(10, radixValue);
You might not always want to parse the integer into a base 10 number, so supplying the radix allows you to specify other number systems.
The radix is the number of values for a single digit. Hexidecimal would be 16. Octal would be 8, Binary would be 2, and so on...
In the parseInt()
function, there are several things you can do to hint at the radix without supplying it. These can also work against you if the user is entering a string that matches one of the rules but doesn't expressly mean to. For example:
// Numbers with a leading 0 used a radix of 8 (octal) before ECMAScript 5.
// These days, browsers will treat '0101' as decimal.
var result = parseInt('0101');
// Numbers that start with 0x use a radix of 16 (hexidecimal)
var result = parseInt('0x0101');
// Numbers starting with anything else assumes a radix of 10
var result = parseInt('101');
// Or you can specify the radix, in this case 2 (binary)
var result = parseInt('0101', 2);
Because if you have a string number like 0700
and want the output to be integer 700
you need to inform parseInt()
that it is a decimal number rather than octal.
console.log(parseInt("0700"));
// 448
// I really wanted decimal (base 10)
console.log(parseInt("0700", 10));
// 700
// What is this? Binary, Decimal, Octal?
console.log(parseInt("0110"));
// 72
// as binary
console.log(parseInt("0110", 2));
// 6
Radix is the base of a system of numeration. There are an infinite number of numeric systems but the ones with which most people are familiar are base 10 (decimal) and base 2 (binary).
Numeric values can be interpreted differently in different bases. For example, the number 10 in binary can be represented as 2 in decimal.
In the case of parseInt()
, the radix allows you to specify the base to be used. By default, a radix of 10 is used.
However, the radix should always be specified, even when using base 10. Consider the case of
parseInt("010")
// Returns 8
At first glance, you may expect the statement to return 10. Explicit use of the radix will help to avoid confusion:
parseInt("010", 10)
// Returns: 10
The radix is the base number of the number system: http://en.wikipedia.org/wiki/Radix
Normally, you only need to specify the radix if you want it to be different from 10. More specifically (from http://www.w3schools.com/jsref/jsref_parseInt.asp) :
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)
Just adding some additional information which clearly answers the question:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
- [...]
- If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
- [...]
Source: MDN parseInt()