JavaScript AJAX Basics (retiring) Programming AJAX Stage 2 Challenge Answer

Didrik Andersson
Didrik Andersson
7,222 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,391 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,222 Points

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