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 - Parsing Numbers

Hi,

I'm watching the Parsing Numbers video, and what I'm seeing in my JS console is different from what's being shown in the video, and I'm wondering why.

In the video, var j = parseInt("012"); yields the number 10. However, in my JS console, I get 12. Just curious as to why this is.

Thanks! Adam

2 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Adam,

Indeed in the latest version of Chrome the following:

var j = parseInt("012")

j does equal 12.

This is because Chrome and other modern browsers ship with ECMAScript 5 and the functionality of parseInt has slightly changed.

Mozilla has a write up here.

Basically you've got to pass in the number 8 saying you want the number parse in octal form, var j = parseInt("012", 8) in order to return 10 now. But as in the video you should always pass in 10 as a second argument to guarantee in all browsers you're getting a decimal back.

Regard Andrew

Ah, this makes sense. Thanks!