So I was having problem with getting the longitude and latitude using the navigator.geolocation and storing it in a global variable. I used alerts at different places to check the flow of the javascript code. I found that the order to be 1, 2, 4 and 3. Why is that? I am really confused.
 $(document).ready( function() {
      var latitude;
      var longitude;
      alert('1');
      if(navigator.geolocation){
        alert('2');
            navigator.geolocation.getCurrentPosition( function(position){
              alert('3');
              latitude = position.coords.latitude;
              longitude = position.coords.longitude;
              geoCoords(latitude, longitude);
            });
         }
      alert('4');
    });
 
    