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

What's the difference between declaring variables before loops and declaring them inside a loop?

For example, what is the difference between...

var guess;

do {
  guess = prompt('Can you please tell me the name of a popular coffee chain?');
} while ( guess != 'starbucks' );

document.write('Yes, that is the correct answer!');

and this....

do {
  var guess = prompt('Can you please tell me the name of a popular coffee chain?');
} while ( guess != 'starbucks' );

document.write('Yes, that is the correct answer!');

3 Answers

Steven Parker
Steven Parker
243,318 Points

Functionally, there's no difference, since in both cases the variable is global.

But the first example is preferred because it's not "good practice" to declare a global value from within a code block and because a variable should only be declared once.

Follow up question then. The only time a variable is not touched then, is when it is declared within a function, correct? I know I kind pivoted the direction of the topic there.

I'll need to do a bit of testing, but off the cuff, I believe the main difference is what scope you will be able to access var guess. Writing up a bit longer form explanation, stand by...

I actually didn't think about that. That would make a lot more sense then. In the first example, the do code block is searching within the global scope for that variable name, so it can give it the value the user inputs.

In the second example, if we were to create another do while loop with the same variable named guess, then it wouldn't interact with the other do while loop because it's in it's own loop. Am I correct with saying that then?

Steven's got the answer to this one. I was mistaken about how var treats block scope. (I use let so often, these days.)

Where this code would start to operate in interesting ways would be if you used let instead of var to declare your variables. Using let guess, the first example would work.

the second example would throw a ReferenceError, since guess was declared in the do block and isn't accessible outside of it. Since document.write is called outside the do block scope, Since the control variable will not be accessible from the loop context, it would throw the error. (Thanks again, Steven Parker.)

from MDN:

"let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope."

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

"...The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global."

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

I don't think I've gotten that far into JavaScript programming to learn more about using the let instead of the var. But I will eventually. Thank you all for the quick responses. They actually did help quite a bit.

Great! Keep at it, and keep asking questions. :)

Steven Parker
Steven Parker
243,318 Points

Since the variable is global, another loop using it would interact with the first one, probably in undesirable ways.

And you can't declare a loop control variable with "let" inside the loop block as it will not be accessible from the loop context.