Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Javascript: access variables global vs local

Read a lot also on Stack Overflow and here about variable scope global vs local, but i can't find a solution, in my case, access the latitude and longitude variables outside the function:

var latitude = "";
      var longitude = "";
      
      // Find location
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else {
      // x.innerHTML = "Geolocation is not supported by this browser.";
      }
      
      function showPosition(position) {
        console.log("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
        var latitude  = position.coords.latitude;
        var longitude = position.coords.longitude;
      }
      console.log(latitude) // no access!

      navigator.geolocation.getCurrentPosition(showPosition);
      
      var mymap = L.map('mapid').setView([latitude, longitude], 15);

Someone can help me out? i hink the problem is a function in a function?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!