Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

What? parseInt('08') returns 0 with Javascript? WHY!

Olivia Rhye
JavaScript code example

Turn a string into a number in Javascript is just

This works for just about everything.... except "08" and "09".

The leading zero in the string tells the Javascript engine that it is an octal number. Because 8 and 9 are not valid numbers in octal, parseInt returns 0. This is expected behavior because they are not valid octal integers and parseInt returns 0 because the first valid number encountered is a zero. – Resource

So, since they aren't octal you need to provide a base parameter. This is just good practice regardless, so make a habit of doing this and you'll never run into this problem.

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy