title: Javascript variable not working outside function
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1225667/how-to-return-ajax-response-text">How to return AJAX response Text?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm trying to get the following code to work. I know there's something I'm not doing right in the variable scope within the function, but I can't figure out what (I'm mainly a designer, I'm quite happy I finally managed to understand a bit what JSON is about). Could anyone nudge me into the right direction? Thanks :)</p>
  
  <pre><code>var list = new Array();
  
  //Twitter
  function twitter(photos){
      $.each(photos.results, function(index, photo){
          if (photo.entities.media){
              list.push(photo.entities.media[0].media_url);
              console.log(list); // working here, returns array
          }
      });     
  }
  var url = "https://search.twitter.com/search.json?callback=?&amp;q=%23pikachu&amp;include_entities=true&amp;count=50";
  $.getJSON(url, twitter);
  
  console.log(list); //not working here, returns []
  </code></pre>