title: Setting local variable in a JavaScript callback function
body: <p>I'm relatively new to JavaScript and I thought I knew how callback functions worked but after a couple of hours of searching the web I still do not understand why my code is not working.</p>
  
  <p>I am making an AJAX request which returns a string array. I'm trying to set this array to a local variable, but it seems to lose it's value as soon as the callback function is executed.</p>
  
  <pre><code>    var array;
  
      $.ajax({
          type: 'GET',
          url: 'include/load_array.php',
          dataType: 'json',
          success: function(data){
              array = data;
          },
          error: function(jqXHR, textStatus, errorThrown){
              alert("Error loading the data");
          }
      });
  
      console.debug(array);
  </code></pre>
  
  <p>In the console, <code>array</code> appears as undefined. Can anyone explain to me why this is not being set and how it is possible to set a local variable in a callback function.</p>