d3.json
is an asynchronous function. That means that the code you pass is not executed immediately, but as a callback after the request for the JSON file returns. That is, a network request is sent for the file, but the normal flow of execution continues.
The console.log(words);
outside d3.json
is simply executed before the call returns and the array is populated.