After loading a JSON object using d3.json and assigning it to a global variable, I encountered an issue where the console returned 'undefined' when printing the global variable. However, typing the global variable in the Chrome console returned the expected result. Essentially, my goal is to load the JSON object and use it outside of the d3.json function. While this may seem like a simple task, I am still new to JavaScript and d3, so any assistance would be greatly appreciated!
Thank you!
Below is the provided code snippet:
<script type = "text/javascript" source='https://d3js.org/d3.v4.min.js'>
var gapminder;
d3.json("D3_jsondata.json", function(data){
gapminder = data;
});
// The above prints in the console when 'gapminder' is typed
console.log(gapminder); // Prints as 'undefined' in Chrome's console
</script>