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 Review Numbers and the Math Object

parseInt instead of parseFloat()?

Would you not have to use parseFloat() instead of parseInt() as the number is a decimal and not a whole number?

2 Answers

Matthew Lang
Matthew Lang
13,483 Points

I believe parseInt and parseFloat can be given any numeric value, however parseInt returns an integer and parseFloat returns a float.

For example:

parseInt(3.141); // Returns 3
parseInt(3.9); // Returns 3 - always rounds down

parseFloat(54); // Returns 54
parseFloat(54.582); // Returns 54.582
Aiden Campbell
Aiden Campbell
6,618 Points

Not 100% sure what question you are referring to, but parseInt() will round down a number completely unless it's an integer already. ex: parseInt(3.77), parseInt(3.5), parseInt(3.11), and parseInt(3) will all result in 3. So it can come in handy depending on the task, and it can be used on floats but the result may not be desired.