title: Update global variable after click event (JavaScript)
body: <p>It sounds super simple, but I am looking to update my global variables ('latitude' and 'longitude') every time the map is clicked, as need to use these values elsewhere. Updating the variables from inside the function does not apply. Thus, the alerts show 'undefined'.</p>
  <p>Any guidance would be much appreciated :-)</p>
  <pre class="lang-js prettyprint-override"><code>// set global vars
  var latitude;
  var longitude;
  var popup = L.popup();
  
  // create function to populate pop-up and global vars
  function onMapClick(e) {  // show pop-up when map is clicked
    popup .setLatLng(e.latlng) .setContent(&quot;You clicked the map at &quot; + e.latlng.toString()) .openOn(mymap); // populate pop-up with lat/long values
    latitude = e.latlng.lat // populate global var to use elsewhere
    longitude = e.latlng.lng // populate global var to use elsewhere
  }
  
  mymap.on('click', onMapClick);  // call function when cliked on map
  
  alert(latitude) // output is 'undefined'
  alert(longitude) // output is 'undefined'
  </code></pre>