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.
Ryan Schmelter9,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.
Steven Parker221,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? will tell ya!
Tray DenneyCourses 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