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

Changed Behavior in "Parsing Numbers from String"

At around 2:21, Jim Hoskins demonstrates how var j = parseInt("012"); will be represented in Octal form, and the console will evaluate to 10 (decimal). This appears to be have changed in the latest version of Chrome, as Chrome Version 30 evaluates it to "12".

3 Answers

The newer versions of Chrome will no longer interpret an integer starting with "0" as an octal number as they use a newer version of ECMAScript. This is explained under the parseInt() documentation on W3Schools:

Note: Older browsers will use the octal radix (8) as default when the string begins with "0". As of ECMAScript 5, the default is the decimal radix (10).

Note: Older browsers will result parseInt("010") as 8, because older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0". In ECMAScript 5, the default is the decimal radix (10).

James Barnett
James Barnett
39,199 Points

Rather than using W3Schools due to it's issues, I'd recommend using HTML Dog, Sitepoint Reference & devdocs. In this particular case here's the article on parseInt from devdocs

I realize that W3schools has its errors but in this case, the explanation Is the same but presented in a simpler format on W3schools.

James Barnett
James Barnett
39,199 Points

I mention it because I think it's a categorically bad idea to send a W3Schools link to a beginner without noting caveat emptor.

That makes sense. By the end of the video, it becomes apparent that the default radix changed. It might be confusing for beginners following along to the video though.

I agree. I think they should add this to a note on the video, as the change to ECMAScript 5 happened recently and any upgraded versions of Chrome will no longer match the video.

James Barnett
James Barnett
39,199 Points

Tagging Jim Hoskins & Nick Pettit about adding a note to the video.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi all,

The thing to take away is at 3:11 is to add a second argument to guarantee the value is in decimal or octal.

If you want your code to work in browsers old or new you should be explicit.

parseInt("012",10);

Regards
Andrew