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 Foundations Variables Shadowing

I don't understand why Nick isn't the global variable.

I understand that using var person="Jim" outside of a function sets it for global scope. I also understand that not using the "var" inside of a function will declare it as a global variable. What I am missing is when person="Nick" is declared outside of a function does that not set it as a global variable? If not then, does not using var always trump non var. Thanks! --------- OK! The next video is on Hoisting and it explained my questions-------

J Scott Erickson
J Scott Erickson
11,883 Points

code example?

NM! Saw your edit. Ha.

1 Answer

person="Nick" outside function will end up becoming global. If there is no var person then one is defined for you the compiler. Kinda crazy.. but I think it does. That is why it is a good idea to have all variables defined with var keywords and at top when it is global and top of function when it is gonna work inside your function only