Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Numbers Parsing Numbers From Strings

Molly Bar
PLUS
Molly Bar
Courses Plus Student 5,925 Points

Parsing Numbers From Strings problem - I think this tutorial is obsolete.

I do not have to put base 10 anymore. when I type the variable in the console, I am getting the # i am looking for without base 10. Also, When I put 23 people, I am getting 23 on the console , not "NaN" (not a number). I wonder how old this tutorial is..

1 Answer

Hello, Molly,

Take a look at the Teacher's Notes under that video. They explain that the default behaviour of parseInt() has now changed. By default, it now assumes the radix is 10, not 8. But passing the radix to the parseInt() function isn't just about getting your code to work, it's also about code readability. Someone else might end up reading code you've written and an explicit radix shows that you are fully aware of the workings of that function.

This change is part of the ECMAScript 5 standard but some implementations still haven't allowed it, so for full compatibility, you should always specify a radix. You can take a look at the documentation on MDN that confirms this.

Finally, if you watch the video again, you'll see that Jim is also getting a number for the string "23 people". However, he gets NaN back for the string "there are 23 people" — a string that doesn't start with a number. That behaviour did not change and isn't likely to change.

In fact, ES6, the upcoming standard of JavaScript, defines a new method Number.parseInt() which acts exactly the same as the global parseInt() function.