0
var feed;
$.getJSON('short.json', function(data) {
    feed = data.items;
    console.log(feed);
    });
console.log(feed);

I have this short code written above. I am expecting feed to be a global variable but once it comes out the function, it's undefined again. It prints out an object when inside. What am I doing wrong?

Thanks for the help.

Mikasa
  • 11
  • 5

1 Answers1

0

The reason is that the getJSON() call is asynchronous. It won't run until AFTER the second console.log();

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71