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 trialEric Garcia
6,217 PointsIs it necessary to create the variable "guess" at the top of the page even though there is nothing inside it?
In the examples, variables are created with nothing inside of them, but then used in the loop further down, can someone explain why this is?
3 Answers
Steven Parker
231,269 PointsA variable should only be declared once, but may be assigned any number of times.
Carlos Lantigua
5,938 PointsI believe it is created as a global variable so that it can be declared and store the information gathered from the user when prompted from anywhere in the script (even in functions where vars are considered local). I was able to get the exact same result by deleting the var guess at the top and placing it into the do statement. If the var effects loops the way that they affect functions, then you may not want to do this if you want to access that variable later on in the code some where.
Steven Parker
231,269 PointsA loop does not affect the scope of a "var". But any kind of block will affect "let" and "const".
Carlos Lantigua
5,938 PointsOk that makes sense, so maybe he just did it to keep the vars together in this case so that we could better grasp what everything is.