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 trialCarlo Sabogal
7,468 PointsWhy do I need to use parseInt? I don't see a difference when I remove it. thanks
Why do I need to use parseInt? I don't see a difference when I remove it.
thanks
3 Answers
William Li
Courses Plus Student 26,868 PointsI don't see a difference when I remove it.
Not quite, it only happens that in this particular case, if the user enters an integer number, the multiplication result of Math.random() * topNumber
is the same regardless of whether you use the parseInt()
.
But in many other cases, it's not.
2 * "32px" // => NaN (Not a number, can't compute)
2 * parseInt("32px") // => 64
1 + "32px" // => "132px" (String concatenation)
1 + parseInt("32px") // => 33
1 + "32.1" // => "132.1" (again, String concatenation)
1 + parseInt("32.1") // => 33
As other answers have said before, user input was passed along as String; but you can't make any assumption on what user might enter. JavaScript is a very forgiving language, it tries to help you out by allowing math calculation using mixed types, and this can lead to lots of unintended outcome of the return result, because how JavaScript decides to deal with these situations may not be consistent, sometimes it converts String to number, sometimes it's the other way around.
Avoid mathematical computation involves mixed types by using methods parseInt
, parseFloat
, if needed, to be on the safe side.
huckleberry
14,636 PointsThe reason you would need parseInt is because when data is submitted by a user it gets submitted/saved/returned as a string.
If you want to use that input for anything other than printing it out to the console -- such as in a mathematical expression or in a comparison -- it needs to be an actual number.
/*This will store the user input as a string and thus you won't be able to use it in any expressions
where you're adding, subtracting, multiplying, dividing or comparing it to another number*/
var userAge = prompt("How old are you?");
/*This will store the user input as a number and thus you WILL be able to interact with it *as* a number
which allows you to do mathematical operations and comparisons*/
var userAge = parseInt(prompt("What's your age?"))
Cheers,
Huck -
Daniel Toader
4,574 PointsInput from the user is usually passed as a String. If you need that input to actually be an integer instead of a String with number(s) inside it, you need to use parseInt to convert it to an integer.