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 AJAX Basics (retiring) Programming AJAX Stage 2 Challenge Answer

Didrik Andersson
Didrik Andersson
7,337 Points

About statusHTML

Hello! I have a question about the statusHTML variable from this video. Maybe it's a dumb question but how can we use var statusHTML in both XMLHttpRequests?

Is this because we are declaring the variable inside of the callback function that the value of statusHTML gets (resetted?) ?

1 Answer

andren
andren
28,521 Points

Is this because we are declaring the variable inside of the callback function that the value of statusHTML gets (resetted?) ?

Yes, that is more or less the right answer.

A variable declared with var has a local scope, also called a function-scope. That means that it only exists within the function it is declared in. Since the two XMLHttpRequests gets assigned different functions you can declare variables with the same name without creating a conflict. So even though they share the same name, the statusHTML in the first callback function is not considered to be the same variable as the one declared in the second callback function.

Didrik Andersson
Didrik Andersson
7,337 Points

Okay, now I get it. Thank you! :)