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 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
221,450 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