Javascript parseInt() with leading zeros

Javascript parseInt() with leading zeros

Javascript’s parseInt function does not seem to completely work.
parseInt(“01”) returns 1
parseInt(“02”) returns 2
parseInt(“03”) returns 3
parseInt(“04”) returns 4
parseInt(“05”) returns 5
parseInt(“06”) returns 6
parseInt(“07”) returns 7
parseInt(“08”) returns 0
parseInt(“09”) returns 0

You can’t explain that. Give it a try. (jsFiddle)
Edit Since this question was asked and answered, the “feature” of defaulting to octal radix has been deprecated. [1] [2]

Solutions/Answers:

Solution 1:

This is because if a number starts with a ‘0’, it’s treated as base 8 (octal).

You can force the base by passing the base as the 2nd parameter.

parseInt("09", 10) // 9

According to the docs, the 2nd parameter is optional, but it’s not always assumed to be 10, as you can see from your example.

Solution 2:

Calls to parseInt should always specify a base in the second argument:

parseInt("08", 10);

Earlier versions of JavaScript treat strings starting with 0 as octal (when no base is specified) and neither 08 nor 09 are valid octal numbers.

From the Mozilla documentation:

If radix is undefined or 0, JavaScript assumes the following:

  • If the input string begins with “0x” or “0X”, radix is 16
    (hexadecimal).
  • If the input string begins with “0”, radix is eight
    (octal). This feature is non-standard, and some implementations
    deliberately do not support it (instead using the radix 10). For this
    reason always specify a radix when using parseInt.
  • If the input string
    begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN.

And from the ECMAScript 3 standard:

When radix is 0 or undefined and the string’s number begins with a 0
digit not followed by an x or X, then the implementation may, at its
discretion, interpret the number either as being octal or as being
decimal. Implementations are encouraged to interpret numbers in this
case as being decimal.

The latest version of JavaScript (ECMAScript 5) abandons this behavior, but you should still specify the radix to satisfy older browsers.

Solution 3:

There is a Radix parameter

parseInt(value, base)

Where base is the radix.

In this case you are evaluating base10 (decimal) numbers,
therefore use

parseInt(value, 10);

Solution 4:

This doesn’t seem to be completely valid in new browsers. Internet Explorer 9 and 10 will return 8 if you execute ‘parseInt(“08”)’ whereas Internet Explorer 8 and previous versions will return 0 (IE10 in quirks mode will also return 0).

The latest version of Chrome also returns 8 so they must have changed the interpretation recently.

Solution 5:

This issue is deprecated now. But you can still use radix in parseInt to convert number of other bases into base-10. E.g.,

var baseTwoNumber = parseInt('0010001', 2);

returns 17 (which is base-10 of 0010001).

Solution 6:

Tip: As you now know when default to octal has been deprecated. Here is how you would fix it in legacy browsers

// ES-5 15.1.2.2
if (parseInt('08') !== 8 || parseInt('0x16') !== 22) {
    parseInt = (function (origParseInt) {
        var hexRegex = /^0[xX]/;
        return function parseIntES5(str, radix) {
            str = String(str).trim();
            if (!Number(radix)) {
                radix = hexRegex.test(str) ? 16 : 10;
            }
            return origParseInt(str, radix);
        };
    }(parseInt));
}