I'm learning javascript but stuck on one thing that, I have created an empty object and trying to mutate its value inside a function, and problem is that when I access the changed value from inside the function it shows but when I try to access object key and value from outside the function I get UNDEFINED
.
Here is the code
"use strict"
//Fetch auto location
let user_location = {};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.log("Geolocation is not supported by this browser.");
}
function showPosition(position) {
console.log(position.coords.latitude, position.coords.longitude)
user_location.lat = position.coords.latitude, position.coords.longitude
user_location.lon = position.coords.latitude, position.coords.longitude
//This shows the data perfectly
console.log(user_location)
};
//But This shows undefined
console.log(user_location.lon)