Last Updated: May 15, 2019
·
782
· 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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside