I'm trying to log the location of the user into an array to user later. In trying to check that it's working I found that the whole array would log to the console, but if I try to log just one element in the array I get an error. Was wondering if anyone could help me understand why.
Javascript is below
window.onload = function getLocation() {
var userlocation = [];
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function showPosition(position) {
userlatitude = position.coords.latitude;
userlongitude = position.coords.longitude;
userlocation.push(userlatitude);
userlocation.push(userlongitude);
}
console.log(userlocation[0]);
console.log(userlocation);
}
I would expect console.log(userlocation[0]) to output the userlatitude but it is coming out as undefined.