I'm trying to store a variable inside a function into a global variable.
But the global variable outside of the function stays undefined.
Code snippet:
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}
var latitude;
var longitude; 
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + 
"<br>Longitude: " + position.coords.longitude;
 latitude = position.coords.latitude                   
 longitude = position.coords.longitude                 
 console.log(longitude);    // var longitude is set here 
}
console.log(longitude);   // var longitude is undefined here
I want to use the variable outside of the function too, how can I fix this?
 
    