0

I know this is something really fundamental, but I am not quiet getting it. I am trying to define a variable abc as global variable and then printing it out at the end. But it returns undefined. Printing it inside works just fine.

var abc;
var data;
$.get('http://localhost:3000/test.php', function (data, status) {
    console.log(status);
    abc = data;
    console.log(abc);  //printing it here works fine
});

console.log(abc);  //printing it here returns undefined
  • .get does not block. Read up on callbacks. – folkol Dec 03 '15 at 22:28
  • You are making an asynchronous call. As such the value will not be defined outside of the get call until after the get returns. – scrappedcola Dec 03 '15 at 22:28
  • Change the first line to `var abc=5;` and see what happens. And change one of your `console.log()`'s to `console.log("123" + abc);` to figure out which one is actually working. – Thomas Landauer Dec 03 '15 at 22:29

0 Answers0