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 trialErin Agobert
4,781 PointsI thought that when "var" isn't declared, the script will ignore the function scope and move onto the global scope.
In this case, since "var message = 'Welcome!'; " is in the global scope AND var isn't declared in the function scope, wouldn't the answer become "Welcome"?
1 Answer
Steven Parker
231,275 PointsNo, because the "var" keyword isn't used to declare a new "message" variable inside the function, the function overwrites the value in the global variable.
Erin Agobert
4,781 PointsThank you Steven!
Nicholas Grenwalt
46,626 PointsNicholas Grenwalt
46,626 PointsSince var is already declared outside the function and the inner function refers to that same variable since it doesn't use the var keyword inside the function that changes the global message variable when setMessage() is called.
Now if the inner function would have been called with the var keyword then the outer variable message would still be 'Welcome!' since now the inner message variable would refer to a different local variable with the same name.
Hope that makes sense.