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

185
Views
How do I access variables from a nested arrow function?

I am storing the data from an API call in variables and I would like to use the data in the variables elsewhere in the function. However, when I printout what is in the variables outside of the arrow function, the data has a type of undefined.

The first console.log(coordinates); displays ["08002", "39.916258", "-75.021428"] and the second displays

[
    "08002",
    "39.916258",
    "-75.021428"
]

But when I have console.log(coordinates[1]); outside of the arrow function it says it's undefined.

const getLongLat = function(zip) { //gets long lat from a zip code
  zip = document.getElementById("ZipCode").value
  let lat = 0;
  let long = 0;
  let coordinates = [];
  if (zip == "") {
    zip = "08002"
  }
  coordinates.push(zip);
  const APIkey = "#########";
  let base = `http://open.mapquestapi.com/geocoding/v1/address? 
key=${APIkey}&location=${zip}`;
  console.log("base " + base);

  fetch(base)
    .then(response => response.json())
    .then(data => {

      //makes it so that the city is in the us
      let i = 0;
      while (data.results[0].locations[i].adminArea1 != "US") {
        i++
      }
      lat = String(data.results[0].locations[i].latLng.lat);
      long = String(data.results[0].locations[i].latLng.lng);
      coordinates.push(lat);
      coordinates.push(long);
      console.log(coordinates);

    })
  console.log(coordinates);
  return coordinates;
};
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!