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

Ryan Schmelter
Ryan Schmelter
9,710 Points

Is there a reason to ever use parseInt when parseFloat does the same thing?

parseFloat will convert an string like "100" into a number. Just wondering why we shouldn't use that all the time.

3 Answers

Steven Parker
Steven Parker
231,271 Points

They do different things.

Sure, they both convert "100", but parseFloat will also convert something like "33.3". On the other hand, parseInt will enforce acceptance of only integer input, and it will also take a second argument that allows you to specify a conversion radix other than decimal.

What does the hex value "F00D" represent? parseInt("F00D", 16) will tell ya!

Tray Denney
PLUS
Tray Denney
Courses Plus Student 12,884 Points

Hey there! I have found some discussions that cover the difference between using the two.

Here are also the docs for the different usages.

parseInt() has an extra parameter called "radix" which is describes as

radix: An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

I hope this helps!

parseInt will convert a string to an integer, something similar to the number 100, while parseFloat will convert a string to a float value, maybe something like 100.00