let preGameRequest = new XMLHttpRequest();
let preGameData;
preGameRequest.open("GET", "/matches/live.json");
preGameRequest.onload = function() {
preGameData = JSON.parse(preGameRequest.responseText);
}
preGameRequest.send();
console.log(preGameData); // issue arises here
I've encountered a problem with my code. I declared preGameData as a global variable and attempted to store the data from the '/matches/live.json' file into it. However, when I attempt to log preGameData outside of its scope (like in the code section), it returns 'undefined'. Strangely enough, logging preGameData from within the scope works perfectly fine. I'm quite puzzled by this behavior and unsure of what exactly is causing it.