You must remember the nature of async \ ajax.
this is the execution order of your code:
var lat; alert(lat); // this alerts null navigator.geolocation.getCurrentPosition(callback,fail,{timeout:10000}); function callback (position) { lat = position.coords.latitude; }
That is why you get null. async! , async! :)
source share