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 Scope

Hassan Kotati
Hassan Kotati
3,645 Points

Can't understande the right answer for the 5th quiz

var part1 = "Team ";

function bam() { var part2 = "Treehouse"; }



I thought this is the same as:

var part1 = "Team ";

function bam() { var part2 = "Treehouse"; }

var part2 = "Treehouse"; //here we are invoking the var part2,

so (var part2) is accessible because it's been called oustside the function

Thus, console.log(part2); print out: Treehouse

1 Answer

William Li
William Li
Courses Plus Student 26,868 Points

No, Hassan Kotati .

var part2 = "Treehouse"; was declared inside the function bam, that makes it the local variable to the function, which means as soon as the function invocation is done, the variable local to this the function is gone as well.

So unlike var part1, which is a global variable that is accessible anywhere, var part2 as local variable, can only be accessed within the function body of bum, NOTHING outside of the function can access this variable.