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

Gaia Dragonfly
Gaia Dragonfly
4,546 Points

A questions about scope in 'while' loops.

So I know there is a global scope and the scope inside the functions, does it apply to while loops? is a variable I am creating inside a while loop is global or unique to this loop?

If it is unique, what do I get if I create a variable in the same name as a global variable, when the the loop runs again will it check my global variable or my unique variable?

1 Answer

Sara Hardy
Sara Hardy
8,650 Points

While loops do not have their own scope. They share scope with the function they live in. Any variable you create within a while loop is available anywhere within that function.

Gaia Dragonfly
Gaia Dragonfly
4,546 Points

Thank you, I think that cleared up things for me :)