Scope: variable declared inside as opposed to outside of a loop

Scope: variable declared inside as opposed to outside of a loop
0

#1

While developing my twitch project, i had code like this:

var language, views="", html = "";
//var status;
  twitchStreamers.forEach(function(streamer) {
   // some code
    $.getJSON(streamURL, function(streamData) {
      var status;
      // some code
if(condition){
status = "some value 1";
}
else if(condition) {
status = "some value 2";
}
else {
status = "some value 3";}
      $.getJSON(channelURL, function(channelData) {
     // some code
     // access value of status
}) // end of second ajax request
}) // end of 1st ajax reuqest
} //end of forEach loop

full code
Notice the status variable. this design works perfectly with what i want to do i.e. when i access the status variable inside the second ajax request, it has the correct value of status according to the current loop iteration. But when i declare the variable outside forEach loop the variable status always has the value “some value 3” (initialised in the else part of the conditionals) when accessed in the second AJAX request. Why is this so?
Note that the variable status is initialised correctly in the first in the first AJAX request irrespective of where the variable is declared.