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 Basics (Retired) Creating Reusable Code with Functions Variable Scope

Abubakr Alhaj
Abubakr Alhaj
4,297 Points

I understood the scope of the function but in the min. 1:23 it's very confusing because when you see the demonstration.

the vid demonstration is very confusing in 1:23

1 Answer

Ali Abbas
Ali Abbas
2,097 Points

Hello Abubakr Alhaj,

I think I know why you might be confused. I'll try my best to help clarify any issues. By calling the greeting() function, it allows him to use the 'var lilah' and 'alert(person)' stored within it. However, in order to use the 'var george', since it's stored outside of the function, it has to be called separately. Hence, why he typed 'alert(person);' after calling 'greeting()'. So, in other words, when calling any function, the arguments inside of it's body don't affect the arguments outside of it.

In other words, greeting() was called this would display lilah because the alert function to call lilah is within the function itself, then he wrote another alert(person) below it, this would display george because this var george was declared outside of the function, then again he typed greeting() below the alert which would once again diplay lilah.

I hope this clarifies it for you and I didn't actually end up making it more confusing lol. Take care.

Ali Abbas,

Your explanation made sense to me. -If we go line by line top to bottom: 1) Function stores Lilah in person variable and will display an alert with that variable, therefore Lilah is displayed. (Function Scope) 2) variable person is created (outside the above function) with the value of George. (Global Scope) 3) greeting function is called, therefore Lilah is displayed (Function Scope) 4) alert(person) is run, therefore since person outside the function is a variable with the value of George, George is displayed in the alert (Global Scope) 5) greeting function is called, therefore Lilah is displayed (Function Scope)

In short, 3 alerts displayed displaying, respectively "Lilah", "George, "Lilah"