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 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,558 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! :)