How to add gelocation to global variable?

I want something like var lat = position.coords.latitude

 function getLocation() {
     if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(showPosition);
     } else { 
         document.getElementById("demo").innerHTML = "Geolocation is not supported by this browser.";
     }
 }
 
 function showPosition(position) {
     document.getElementById("demo").innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
 }
 
 document.getElementById("btn").addEventListener("click", getLocation);

Thanks. I’ll keep it in mind.

But now I have CORS errors.
I’ve tried several proxies and it still doesn’t work.

Thanks. I’ve switched to the FCC weather API instead, but I’m still getting 401 error, and then CORS error on top. Is this common with these FCC projects?