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

why is show the string Lilha, George than Lilha angain!!

I watch the Variable Scope video again and again , I never understand the concept about the third called function greeting() .Why it show up the string Lilha, George than Lilha , can someone help me with it ? I'm confuse. the example is like this , function greeting() { var person = 'Lilah'; alert(person); } var person = "George";

when call the first function: , greeting() it show up with a alert dialog said Lilah, I know why ,alert(person) it show up a alert dialog with George , I understand but when , greeting() call, , i have no idea why it show the string Lilha, George and than Lilha Again, , can someone really explain why ? I very appreciate that!

2 Answers

Hi there,

Let's start with this, from the video:

function greeting() {
  var person = 'Lilah';
  alert(person);
}

var person = 'George';
alert(person);
greeting();

We have two scopes here. One is the global scope, and the other is the scope specific to the greeting() function. At the bottom, we create a variable called "person" that has George stored in it - that one is in the global scope. The "person" variable inside greeting(), set to Lilah, is creating a new variable inside that function's scope, so they are two separate values, and which one you get will depend on where you use "person". If you use that value inside the function, you'll get Lilah, and outside, you'll get George.

This is illustrated by the fact that the alert(person) line under the 'George' person declaration will return George - it is outside the function and in the global scope, where person is set to George. If you call the greeting() function, you'll get Lilah, because the alert(person) for greeting() is inside the function - it will access the "person" variable in the function's scope first, so you get Lilah.

Now, if I read your question correctly, this is the part that was confusing - in the video, he removed the "var" from the line inside the function where he sets person to Lilah, so now the code looks like this:

function greeting() {
  person = 'Lilah'; //right here - no more 'var'
  alert(person);
}

var person = 'George';
alert(person);
greeting();

Something important happens when you remove "var" there - without "var", you are no longer creating a new variable inside the function's scope - you are setting the existing "person" variable in the global scope to Lilah. I'm going to add a line of code at the bottom to illustrate the difference. So, when these four lines run:

var person = 'George';
alert(person);
greeting();
alert(person);
  • Line 1 creates a "person" variable, set to "George".
  • Line 2 does an alert box with "person" in it - George will come up
  • Line 3 runs greeting()
  • Line 4 does an alert box with "person" in it - Lilah will come up

So, why would Lilah come up on the second alert(person)?

function greeting() {
  person = 'Lilah';    //Line 1
  alert(person);
}
  • Line 1: Without a "var" keyword, it will look for an existing "person" variable instead of creating a new one - it finds one, in the global scope. The variable that was set to "George" is now set to "Lilah"

So, when we get to the second alert(person), the variable is now Lilah, because we have run greeting(), which changes that variable's value.

Does that make sense?

Rhys Kearns
Rhys Kearns
4,976 Points

Please lay it out so its somewhat readable like -

function greeting () {
  var person = 'Lilah'; 
  alert(person); 
}

Thank you i don't know how , but Thank you