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

Enemuo Felix
Enemuo Felix
1,895 Points

Just really want to know if I'm on the same page with this

Does the alert function still access person = Lilah; without calling the callName(); function first ?

var person = George;
function callName () {
  person = Lilah;
}
alert(person);

Will the alert run George or Lilah?

Karolin Rafalski
Karolin Rafalski
11,368 Points

The best way to do it is to test it! You can open up chrome developer tools by pressing command option i or choosing from the menu -> view -> developer -> JavaScript Console

Then copy paste the code in there. There are a lot of other ways to run JS code too! Just use the one you feel most comfortable with.

One thing is that George and Lilah should be strings, so you will need to wrap those in quotes.

Once you find out - write back what you got and let us know if it makes sense :-)

1 Answer

Enemuo Felix
Enemuo Felix
1,895 Points

Thanks karolin. I actually did as you suggested and got George on a display dialog and also did another test but this time i introduced the callName(); function first before the alert (person); function and got Lilah on a dialog display. So i noticed by calling the callName(); function first before the alert that it cancel out the Global scope (George) and access the global without the var keyword. Like this

var person = George;
function callName () {
  person = Lilah;
}
callName ();
alert (person);

So this displayed Lilah