I can access the array words
in global scope but when I try to find an item by typing words[index]
, it returns undefined. How can I solve this?
let words = [];
function fetchWords() {
fetch("https://www.themealdb.com/api/json/v1/1/categories.php")
.then((res) => res.json())
.then((data) => {
for (let i = 0; i < 13; i++) {
words.push(data.categories[i].strCategory);
}
});
}
fetchWords();`
console.log(words); //This works
console.log(words[2]); // But this does not. Why?