I have a file data.json
with the contents: {"id": "foo", "value": "bar"}
. I have another file index.html
in the same directory with the following contents:
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var datavar;
d3.json("data.json", function(d) { datavar = d; console.log(datavar); });
console.log(datavar);
</script>
When I open index.html
in a web browser, the first console.log(datavar)
call outputs {id: "foo", value: "bar"}
. The second one outputs undefined
. I was under the impression that since I initialized datavar
outside of the function, changes I make to it would last once we're back out of the function. How can I store data from within the function that lasts outside the function?