Can anyone tell me why “zip” is still undefined after defining it in the .getJSON function? http://codepen.io/david_vu408/pen/JErXdW
The getJSON is asynchronous so your alert returns BEFORE that zip assignment, u should give zip a default value while waiting for getJSON to give u back the data.
Try
var zip = something ;
$.getJSON("http://ip-api.com/json/?callback=?", function(data) {
zip = data.zip;
});
window.alert(zip);
Or
var zip = something ;
$.getJSON("http://ip-api.com/json/?callback=?", function(data) {
zip = data.zip;
window.alert(zip);
});