parseInt("08") (no radix) returns 8 in Chrome and Firefox

Viewed 63

I was showing my colleague why you must always specify a radix with parseInt and I was surprised to see that parseInt("08") returns 8 in both the console of Firefox and Chrome

shouldn't it be an octal and return 0?

parseInt('08') returns 8

1 Answers

This is a question for old timers

Before 2011, parseInt("08") used to return 0 as parseInt would determine 08 was an octal as it started with an 0

As @Pointy pointed out, the radix being octal for string starting with a 0 has been deprecated for a long time now

As the radix used in this case depends on the implementation, you should specify a radix (parseInt("08", 10) because there is no way to be sure whether the octal or decimal algorithm will be used

Sources:

Related