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 Numbers Working with Numbers Convert Strings to Numbers

When to parseInt()

Are there any disadvantages to converting the input directly to an integer if you know that it will only be used for number operations? For example in this video is there anything wrong with doing something like

const HTMLBadges = parseInt(prompt("How many HTML badges do you have?"));

over something like

let HTMLBadges = prompt("How many HTML badges do you have?");
HTMLBadges = parseInt(HTMLBadges);

2 Answers

Steven Parker
Steven Parker
231,248 Points

There's no difference if you aren't going to be doing any error checking. But if you were, it might be nice to keep the original string so that you could try other operations on it and/or repeat it back to the user in an error message.

The disadvantage of this: const HTMLBadges = parseInt(prompt("How many HTML badges do you have?"));

If the user entered let's which is not number, the HTMLBadges will never get any value.

Because, it was defined as the " const ", so that value will never able to change.