Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Numbers Parsing Numbers From Strings

Parsing Numbers From Strings

I keep getting 12 instead of 10. I am kind of lost

var j = parseInt("012");

2 Answers

Ian Svoboda
Ian Svoboda
16,639 Points

If you're trying to return 10 from that snippet, you'd need to pass in the radix (8) after your number:

var j = parseInt("012",8);

source: http://www.w3schools.com/jsref/jsref_parseint.asp

Syntax parseInt(string,radix) Parameter Values Parameter Description string Required. The string to be parsed radix Optional. A number (from 2 to 36) that represents the numeral system to be used

Thank you for the reply! :)

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

There's a teacher's note that was added after the video was created:

Please note that newer browsers will return 12 for parseInt("012") and not 10. This is because the default second parameter is now 10 for decimal instead of 8 for octal. Always specify the base if you're parsing a string to maximize compatibility.

parseInt("012", 10); // For decimal. Returns 12
parseInt("012", 8); //For octal. Returns 10