Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,867 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.