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 Hoisting

Trang Ho
Trang Ho
3,518 Points

Variables and Shadowing: I don't understand why these two functions in Quiz Questions 1 and 2 have different results

Quiz Question 1 of 2 What will console log out in the following code?

var person = "Jim";

function whosGotTheFunc() { person = "Andrew"; }

person = "Nick";

whosGotTheFunc();

console.log(person);

Quiz Question 2 of 2

What will console log out in the following code?

var person = "Jim";

function whosGotTheFunc() { var person = "Andrew"; } person = "Nick";

whosGotTheFunc();

console.log(person);

Why is the first answer Andrew and the second one Nick? The two codes are the same to me.

3 Answers

Colin Marshall
Colin Marshall
32,861 Points

This has to do with variable scope. In the 2nd question, there is a "var" in front of the "person" variable inside the function when it is set to "Andrew." Because of that "var" you are defining a new variable called person inside the function. This is different from the globally declared "person" variable (first line of the code). In the 2nd quiz question there are essentially two different variables, both with the name "person." Any variable declared inside a function can only be accessed inside the function.

Hugo Paz
Hugo Paz
15,622 Points

Hi Trang,

The key difference is the variable person inside each function.

When you declare a variable inside a function, that variable is only accessible inside the function (scope).

So when you look at the first question, you declare the variable person outside any function, this means its scope is global - the variable can be accessed anywhere in your code.

So running the script we have:

first value assignment var person = "Jim";

second value assignment person = "Nick";

then we call the function whosGotTheFunc, this sets person to Andrew. As you can see inside the function, its just person = 'Andrew', so its targeting the variable person declared in the global scope.

In the second question, the key difference is inside the whosGotTheFunc, you declare a new variable called person - var person = 'Andrew'. This variable is only visible inside the function. Even though it has the same name as the one declared outside the function, they are different variables. Once the function finishes running, the variable declared inside disappears

Just to give you an example, if you wanted to make the second question give the same result as the first, you could do it like this:

var person = "Jim";

function whosGotTheFunc() { 
var person = "Andrew"; 
return person;
} 

person = "Nick";

person = whosGotTheFunc();

console.log(person);
Trang Ho
Trang Ho
3,518 Points

Thank you.

Why isn't Nick the result in Question 1?