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 Shadowing

Ingrid Bardales
Ingrid Bardales
10,616 Points

which variable console's out

What will console log out in the following code?

var person = "Jim";

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

person = "Nick";

whosGotTheFunc();

console.log(person);

The answer to this quiz question is "Jim" and i thought it would have been "Nick". Here's my logic: Jim is initialized in the global scope, Andrew is initialized in the local scope of the fuction, Nick is now the new value of the var "person" the function is called and the value is Andrew, console.log(person) --this should console out the new value of Nick.

Ok pleeeease, tell me where i went wrong, this is eating at me, thanx in advance for your help!!! Ingrid

5 Answers

Joy Kesten
STAFF
Joy Kesten
Treehouse Guest Teacher

I got "Nick" as the correct answer when the var keyword was written before the variable person inside the function because in this case it's making a new variable inside the function.

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

I got "Andrew" as the correct answer when var was not included before person inside the function because in this case it thinks you're trying to assign a new value to the global variable person.

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

When a variable is defined in the global scope and then assigned the same named variable inside a function using the var keyword + variable name+ value, the variable inside the function is only going to be accessible inside the function.

In short, by defining the variable in the function, you do not affect the global variable. If you leave off the var you're reassigning the global variable.

Ingrid Bardales
Ingrid Bardales
10,616 Points

Thanks Joy,...but here is what i thought"

OK, I know var person ="Jim" is declared on the global scope and i also understand that the var person = "Andrew" inside the function is not going to called because it is local to the function but then after the function, you have person = "Nick", I thought Nick would then replace "Jim" .

The var declaration inside of the function is whats throwing you off.

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

This function creates a new scope in which the var person is defined and set to andrew. This does not affect the global scope person.

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

this function creates a new scope as well; however, instead of defining a variable person and assigning it the string "Andrew"... we are setting the global variable person to andrew.

So in short... Unless the variable is defined first inside of the function with the 'var' keyword, you are writing to the global namespace.

Ingrid Bardales
Ingrid Bardales
10,616 Points

Thanks Matthew,...but here is what i thought"

OK, I know var person ="Jim" is declared on the global scope and i also understand that the var person = "Andrew" inside the function is not going to called because it is local to the function but then after the function, you have person = "Nick", I thought Nick would then replace "Jim" .

Joy Kesten
STAFF
Joy Kesten
Treehouse Guest Teacher

Ingrid Bardales When I took the test, Nick does replace Jim when var is included. Andrew is the answer when var is not included.

Ingrid Bardales
Ingrid Bardales
10,616 Points

oh, when i took the test, i clicked on Nick and it was the wrong answer. then i took it again and got Jim as the right answer..maybe a glitch.

Joy Kesten
Joy Kesten
Treehouse Guest Teacher

weird. Sorry about that. But good work sticking with it and seeking help :) :thumbsup:

Ingrid Bardales
Ingrid Bardales
10,616 Points

thanks guys for your help, it appears as if there was a little glitch when i took the quiz! Thanx again!