Last Updated: May 15, 2019
·
640
· tomasznoworyta

Strange results of parseInt explained

Method parseInt outputs strange results when we try to convert certain strings that begin with "0".

parseInt("08") //=> 0
parseInt("09")  //=> 0

The reason of it is that method parseInt tries to guess the base of numeric system. It incorrectly assumes octal base here.

However the base of the number can be specified as a second parameter:

parseInt("08", 10) //=> 8
parseInt("09", 10)  //=> 9