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 Basics (Retired) Working With Numbers Numbers and Strings

What about just using Number to convert to a number? var age = Number(prompt("How old are you?"));

I think this works too, but maybe not as precise?

2 Answers

According to this answer on stack overflow:

console.log(Number("500px") );          /* output is NaN */
console.log(Number.parseInt("500px") ); /* output is 500 */

Oh, I think I see.. Number will work if it is a string that ONLY contains numbers, but not if there are other things attached. If someone answers '30' as their age in the prompt it should work, but if they answered '30 years old' it would only work as parseInt. Thanks for your reply.