Help with Weather App and functions

For some reason, the variable for latitude is undefined outside of the getJSON function. The prototype of variable is outside the function, then inside I modify the value but when I use the console to check it it says undefinded (when used outside of the function).

I know there might be an easy solution to this but I genuinley tried searching for it but couldn’t find anything.

Here’s the code:

That’s because the getJSON is asynchronous function - JavaScript doesn’t wait for it to finish. Your program is getting to the console.log before that variable is defined.

There are a couple solutions. The easiest is to put the code inside the getJSON callback function.

$.getJSON("", function(data){
  lat =;
  lon = data.lon; 
   * put rest of code that needs lat and lon here