I have below code, I declared valuables globally in the file, and then I assigned value to the variable with in function. but when I tried to read the variable outside the function it gave undefined.
let latitude, longitude, IPLocation;
start(path)
console.log(IPLocation) // not work
async function start(path) {
IPLocation = await getData(path);
latitude = IPLocation.location.lat;
longitude = IPLocation.location.lng;
console.log(IPLocation) // work fine
}
async function getData(path) {
const data = await fetch(path);
const scrampled = await data.json();
parsed = await JSON.parse(JSON.stringify(scrampled));
return parsed
}
why that happen?