I am new to Javascript and am trying to pull geolocation data (longitude/latitude) from the browser. I am trying to understand the scope of the let variables lat and long.
When printed to the console, I receive the following output:
3: undefined
2: undefined
1: 40.112
Why do I receive undefined in the console at all?
window.addEventListener('load', () => {
let lat;
let long;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
lat = position.coords.latitude;
long = position.coords.longitude;
console.log('1: ' + lat)
})
console.log('2: ' + lat)
}
console.log('3: ' + lat)
})