0

let arr = [];

navigator.geolocation.getCurrentPosition(
  function(position) { // success cb
    console.log(position);
    arr.push(position.coords.longitude);
    arr.unshift(position.coords.latitude);
  },
  function() {
    return " ";
  });

console.log(arr); //logs the output just fine

let x = arr[0]; //gives undefined
let y = arr[1]; //gives undefined

Anytime I try to access the arr array, it gives me undefined. I want x to be equal to the longitude co-ordinate and y to the latitude co-ordinate.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dynamix23
  • 53
  • 8
  • 2
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – jabaa Oct 22 '21 at 08:11
  • 2
    `logs the output just fine` No, it says `newArr is undefined` – Jeremy Thille Oct 22 '21 at 08:11
  • "_logs the output just fine_" No, it _appears_ to log it fine due to how the console output is evaluated. See [Weird behavior with objects & console.log](https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log). In reality, at the moment you log it, it is empty. (The duplicate linked above your question explains why and how you should resolve it.) – Ivar Oct 22 '21 at 16:29

0 Answers0