Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:59:13+00:00 2026-06-15T15:59:13+00:00

I’m really struggling with basic Javascript here… basically, I’m trying to make a function

  • 0

I’m really struggling with basic Javascript here… basically, I’m trying to make a function that, when called, sets two variables (longitude and latitude) so that I can run other functions straight afterwards that uses these values.

When I try to alert out the longitude value however, it comes back undefined.

Here’s my code.

var latitude;    
var longitude;

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(displayLocation);
  } else {
    alert("Geolocation is not supported by this browser.");
  }
}

function displayLocation(position, latitude, longitude) {
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;
  return;
}

function newLocation(longitude) {
  alert(longitude);
}

window.onload = function() {
  getLocation(); 
  newLocation();
}

Any help will be seriously appreciated! Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:59:14+00:00Added an answer on June 15, 2026 at 3:59 pm

    There are a few problems with the code you posted:

    The arguments to displayLocation are hiding your global variables. When you make the assignments here, you are actually assigning to your argument variables, which are in the local scope.

    function displayLocation(position, latitude, longitude) {
      latitude = position.coords.latitude;
      longitude = position.coords.longitude;
    }
    

    IIRC, the callback to geolocation.getCurrentPosition only takes the first argument, so you shouldn’t have to define the latitude and longitude as arguments.

    Same problem in the newLocation function. You call it without arguments, but the longitude argument is “hiding” the global variable.

    These are small syntax problems. However there is another problem in the code, which is a bit trickier to solve.

    When the page is loaded, you call the two functions sequentially:

    window.onload = function() {
        getLocation(); 
        newLocation();
    }
    

    The second function, newLocation, expects that getLocation has set the global variables. This, however, may not be the case. When the getLocation function calls geolocation.getCurrentPosition, it’s performing an asynchronous operation. The next line after the call continues to execute immediately, but the callback function displayLocation has not necessarily been called yet. This can be a bit complicated to understand at first, but basically you need to only call newLocation after displayLocation has run.

    So it gets complicated? That’s why it’s considered a good practice to try to avoid global variables altogether. Javascript often forces us to do asynchronous programming, and trying to understand all the possible states in which the global variables could be at any given time could drive you mad.

    Instead, if possible, you should always use function arguments directly. For example in your scenario, you could skip the displayLocation step entirely, and go straight to newLocation:

    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(newLocation);
      } else {
        alert("Geolocation is not supported by this browser.");
      }
    }
    
    function newLocation(position) {
        alert(position.longitude);
    }
    

    So global variables are no longer needed.

    I’m sure that the example code you posted is simplified, and your actual code is more complicated, but if you’re able to follow these principles, I think you’ll have a better time with javascript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.