title: javascript, $.ajax, variable name
body: <p>I'm trying to iterate over an array and assign a variable with a for loop. So something like this:</p>
  
  <pre><code>function Person(name, status){
    this.name = name;
    this.status = status;
  }
  
  var status = [];
  var array = ["bill","bob","carl","ton"];
  function exAjax(function(){
   for(var i = 0; i &lt; array.length; i++){
     var name = array[i];
     console.log(name); =====&gt; this gives the correct name
  
     $.ajax({
       url: xxxxxxx,
       success: function(data){
         if(data.stream === null){
           var person = new Person(name, "dead");
           console.log(name); =====&gt; return undefined until the last
           person
  
           status.push(person);       
         }
       }
  
     })
     name = "";
   }
  })
  </code></pre>
  
  <p>The problem I'm having is that name is not getting into the success function. I thought js keeps traveling upwards to look for the variable if it doesn't exist in it's current scope? I'm getting undefined for the name variable if I try to console.log name! Scope masters what am I doing wrong?</p>