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 trialJeff Lange
8,788 PointsPurpose of parseInt?
The purpose of "parseInt" was defined as "getting the value of an integer out of a variable." However, can someone explain why we would need to do that? Take this example based on the code challenge I just completed:
var Q = "150";
var R = parseInt(Q);
The reason I'm confused is...couldn't we just have set variable Q to 150 (without quotes) in the first place? It basically seems like we went to extra trouble for no reason.
3 Answers
Ben Rubin
Courses Plus Student 14,658 PointsYes, you could have set Q to 150 without the quotes. The question is just trying to demonstrate the difference between string and number variables, and show how you convert a string variable into a number variable. In a real program you'd never do what they're doing here. It's just a made up example for demonstration purposes.
Ben Rubin
Courses Plus Student 14,658 PointsLIke you said, you'll get better with time. There are tons of ways to do anything in a program, so it's up to each programmer to find their own ways of going about things. A lot of the examples on Treehouse are just there to demonstrate a concept in isolation, so they might not be precisely how you'd do something in the real world. The more code you write, the more you'll figure out how you like to do things.
Jeff Lange
8,788 PointsYes, looks like I have Padawan Syndrome...ready to understand and do more than I'm actually ready to, haha. Thanks again, Ben, I appreciate the insight! :)
James Gill
Courses Plus Student 34,936 PointsJeff,
In your example, you'd use parseInt because 150 is a string (and parseInt converts it to an integer). Yes, if you removed the quotes (making it an integer), you wouldn't need to use parseInt. It's as simple as that. But--it won't hurt anything.
Jeff Lange
8,788 PointsI understood that part, it just didn't make sense to me why we would bother doing that if it was easier to just leave it out of quotes. See Ben's answer above--basically it was just an example not meant to actually be practical.
Thank you for stopping in to answer though, I appreciate all the help I can get! :)
Jeff Lange
8,788 PointsJeff Lange
8,788 PointsAh...ok that helps loads. Thanks Ben! :)
I often get confused when the code examples we type out don't actually serve a practical purpose. It's difficult for me to see ahead to what it might do, so I'm sitting there wondering "what is this doing right NOW?" haha. If you have any tips on that I'd appreciate it, but maybe it's just one of those things that I'll get better at with time. :D