JavaScript’s parseInt - Optional Parameter

Today a co-worker and myself had a “duh” moment. My co-worker was having a few problems with ‘parseInt(”000800″)’ returning a value of “0″, however, ‘parseInt(”800″)’ would return a value of “800″. This had me boggled too, as I always though “parseInt” operated in base10. As you can probably tell on where I am going with this, “parseInt” does not solely focus on base10, it can actually do any base (from 2 on up). When you do not supply the second optional parameter, it does its best to guess which base you may be referring to, and from the above results, the guessing could leave you in a state of mind boggling as it wasn’t the results you were expecting.

To solve the problem we simply change the code to ‘parseInt(”000800″, 10)’ and we got the result of “800″. Who knew “parseInt” had a second optional parameter? I was surprised.

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment