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

nick is the correct answer. but why??

why is nick correct???

person="nick" was set outside the function. person was not called variable.

so it must end up in a failure notice: person is not defined.

or not???

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points
var person = "Jim";

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

person = "Nick";

whosGotTheFunc();

console.log(person);

Is this the code you're referring to? Nick is certainly the correct answer. Let me say a couple things about it.

  1. person variable was declared in the first line, person = "Nick"; just reassign it a different value.
  2. the statement var person = "Andrew"; inside the function whosGotTheFunc create another person variable local only to its function scope, which means this person variable is gone as soon as function invocation finished, and has nothing to do with the Global person variable outside of the function.