JS value of variable is not seen after if statement

Hi,

I have simple JS code to get geolocation, geolocation is working, but the value is not stored in the variable I declared before if. They are seen if access from if but not once if is closed.

Please give me advice what I am doing wrong, thx here is my code:
$(document).ready(function()
{
var myLatitude = “”;
var myLongitude = “”;

if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position)
{
//$("#data").html("latitude: " + position.coords.latitude + "
longitude: " + position.coords.longitude);
myLatitude = position.coords.latitude;
console.log("In if: " + myLatitude + " " + myLongitude);
myLongitude = position.coords.longitude;
});
}

console.log("After if: " + myLatitude + " " + myLongitude);
});

Getting the location is an asynchronous operation. So the JS engine will carry out that operation when it can fit it in (i.e. at some point in time the device will return the location coordinates, JS will not pause the program until that happens) Console logging can happen immediately, so the JS engine will do that first while it waits. If you log inside the callback, you should get the location values.