title: Ajax saving array into global variables not working keep getting undefined
body: <p>Trying to save an array retrieved from ajax into a global variable such that I may use it later on but keep getting undefined error</p>
  
  <pre><code>   &lt;script&gt;
      var items = [];
  
      function add(value){
          items.push(value);
      }
  
      $(document).ready( function() {
      $.ajax({
          type: 'POST',
          url: 'xxxx.php',
          dataType: 'json',
          cache: false,
          success: function(result) {
              for(i=0; i &lt; result.length; i++){
                  add(result[i]);
              }
          },
      });
      });
  
      document.write(items[1])
  
      &lt;/script&gt;
  </code></pre>