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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Exiting Loops

Ian Hock
Ian Hock
7,604 Points

I was wondering why we create an empty variable called guess; ? I've erased it and the program still functions?!

Originally I thought guess; stored the value of whatever they enter in the prompt. But the way that block evaluates guess in the (while, do/while, ) loop structure it seems like it shouldn't need it.

3 Answers

Steven Parker
Steven Parker
229,783 Points

Assigning a variable that has not been declared causes it to be created as a global. While this is perfectly legal in terms of the language itself, it is considered a "best practice" to define all variables before they are used. This can be helpful in determining the intended scope of the variable.

JavaScript also has a "strict" mode, and when it is used it makes it illegal to use a variable that has not been declared. So developing the habit of always declaring your variables will simplify development if you ever work in strict mode.

your program might work differently on larger projects as it declares a global variable and global variable may change if you use another variable with same name;