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

Michal Janek
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michal Janek
Front End Web Development Techdegree Graduate 30,654 Points

Why Lilah 3rd time?

I am sorry I tried reviewing the video several times but I just cannot figure out, why there is a 3rd Lilah alert at 1:30 min of the Scope video. Maybe I am too tired at this point but I do not really see that part which calls for third Lilah Alert.

Was it because first calling greeting(); created "3" variable with name person so our second calling of that function makes it read the first calling?

Justin Olson
Justin Olson
13,792 Points

It's easy to miss how this works, especially if you are tired. ;)

Basically, there are three parts being called.

greeting(); This initiates the greeting() function, which asks to alert(person); which is Lilah

alert(person); Since the greeting() function just ran, next is this, asking to alert the variable, person. As this is outside the function, it stands alone, and will come back with George.

greeting(); Lastly, the greeting() function is called one more time. Like before, it runs the same code. As the variable for person INSIDE the function is Lilah, Lilah will be the name called.

These will fire in this order, which is why you will see Lilah, George, then Lilah again. Hope this helps out. :)

2 Answers

Justin Olson
Justin Olson
13,792 Points

It's easy to miss how this works, especially if you are tired. ;)

Basically, there are three parts being called.

greeting(); This initiates the greeting() function, which asks to alert(person); which is Lilah

alert(person); Since the greeting() function just ran, next is this, asking to alert the variable, person. As this is outside the function, it stands alone, and will come back with George.

greeting(); Lastly, the greeting() function is called one more time. Like before, it runs the same code. As the variable for person INSIDE the function is Lilah, Lilah will be the name called.

These will fire in this order, which is why you will see Lilah, George, then Lilah again. Hope this helps out. :)

Edit: forgot to put this an an answer. :)

Amber Touch
Amber Touch
2,339 Points

Haha this is far from obvious. I still don't get it myself.