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 Numbers Working with Numbers Convert Strings to Numbers

Jennifer Ciaccio
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Ciaccio
UX Design Techdegree Student 11,137 Points

What about Number()?

There seems to be so many ways to do the same thing. From my understanding Number() would also provide the same results as the methods mentioned. Is that correct, and if so why would would we use Number() opposed to any of the others?

2 Answers

Steven Parker
Steven Parker
231,248 Points

I assume you're comparing "Number" to "parseInt", and they are not quite the same. "Number" is a more primitive conversion and won't extract values from a compound string. For example:

var x = Number("123test");    // this produces NaN
var y = parseInt("123test");  // but this produces 123

Also, "parseInt" takes an optional second argument to allow it to convert from a radix other than decimal.

For more details, see the MDN pages (my favorite go-to reference) on Number and parseInt.