I'm trying to store the lines of a local file as an array (where each line is a separate element). When I try to access the array later, I recieve an error, saying that the array is undefined.
var lines;
fetch("textfile.txt")
.then((response) => {
return response.text();
})
.then((text) => {
lines=text.split('\n');
console.log(text); //Prints the text in the file
console.log(lines); //Prints the correct array
});
console.log(lines); //Print undefined
How can I fix this so I can use the lines variable elsewhere. Thanks in advance for any help.