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

  • Home
  • SEARCH
  • 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 5981033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:53:11+00:00 2026-05-22T21:53:11+00:00

When I alert my array markerArray outside the geocoder-function it says it’s undefined. Can’t

  • 0

When I alert my array “markerArray” outside the geocoder-function it says it’s undefined.
Can’t figure out why? Is there a way to get the values from the array outside the function?

var markerArray = new Array();
for(var i in opts.markers)
{
    address = opts.markers[i].address;
    //alert(opts.markers[i].icon);
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({ address: address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK && results.length) {
            if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map
                });
            }
        }
        markerArray[i] = marker;

    });

}
alert(markerArray[0].position);
  • 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-05-22T21:53:12+00:00Added an answer on May 22, 2026 at 9:53 pm

    I suspect it’s not markerArray that it’s complaining about, but markerArray[0] that’s undefined.

    You’re calling an asynchronous API using functions you create in a loop. Those functions are closures. They each have an enduring reference to the i variable, not a copy of the value as it was when the function was defined. So all of the functions use the last i value from the loop, because none of them runs until the loop is over. So if the last value i has in the loop is 5, say, then all of the functions will use 5.

    Also, you’re then doing your alert too soon, before any of the callbacks has a chance to run. You’ll need to do whatever final processing you need to do in one of the callbacks (you can use a counter to know when they’ve all happened).

    You can fix both the markerArray problem and the premature alert like this:

    var markerArray = new Array();
    var callcounter = 0;
    for(var i in opts.markers)
    {
        address = opts.markers[i].address;
        //alert(opts.markers[i].icon);
        var geocoder = new google.maps.Geocoder();
    
        ++callcounter;
        geocoder.geocode({ address: address }, buildCallback(i));
    
    }
    
    function buildCallback(index) {
        return function(results, status) {
            if (status == google.maps.GeocoderStatus.OK && results.length) {
                if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                        position: results[0].geometry.location,
                        map: map
                    });
                }
            }
            markerArray[index] = marker;
            if (--callcounter === 0) {
                // This was the last outstanding call
                alert(markerArray[0]); // Always assuming there was a `0` in `opts.markers`
            }
        };
    }
    

    Now, the callbacks are closures over the index argument you pass into the buildCallback function, instead of the i variable in your main loop. We do the alert when we’re done with all the callbacks, which we know because of the callcounter (see note below if your “race condition” radar is going off).

    All of this is because of the way closures work. They’re not complicated (in fact, I wrote a blog post about them called Closures are not complicated), but there are some things you need to firmly understand to “get” why they do what they do.

    Separately: You’re using for..in to loop through opts.markers, which I suspect is an array. If it is, then that code has issues you need to solve. for..in is not for looping through the indexes of an array, it’s for looping through the property names of an object. More here. You either need to add some checks to your for..in loop, or just use a boring old-fashioned for loop.


    Re the counter: To anyone used to multi-threaded programming, my simple “increment it when scheduling, decrement it when processing” logic looks like it sets up a race condition (what if the first one gets called back before the second one is scheduled?). But it’s not a race condition in JavaScript on browsers, because JavaScript on browsers is single-threaded (or if you use web workers, kind of cooperatively multi-threaded). There is no pre-emptive multi-threading here. None of the callbacks will ever be called until they have all been scheduled.


    Off-topic: Although var markerArray = new Array(); works just fine, I’d recommend var markerArray = []; instead. It’s shorter; for various reasons the implementation can optimize it a bit more (not that it really matters); and there’s no possibility that someone has shadowed the Array symbol. Similarly, any time you want just a blank object, use {} instead of new Object().

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

Sidebar

Related Questions

var down=function(a,b){alert(a)} Array.prototype.sort.call(table.tBodies[0].childNodes,down) Array.prototype.sort.call([0,1,2,3],down) Why do I not get alerts from the first sort
Are there any Alert (or Notification) frameworks in Java out there. In my web
var shell = function (method) { window[method].apply(null, Array.prototype.slice.call(arguments, 1)); }; shell('alert', 'monkey!');
function MyObject(){} Array.prototype={}; MyObject.prototype={}; var a=new Array(); var b=new MyObject(); alert(a.constructor==Array);//true alert(b.constructor==MyObject);//false
I have an array: data.Dealer.car[0] data.Dealer.car[1] data.Dealer.car[2] If I do this: alert(data.Dealer.car.length); delete data.Dealer.car[1];
How can I alert the selected day into a format like 1,2,3,4,...,31. I.E. 12.10.2009
function f() { } alert (f.prototype); // returns something like [object Object] My understanding
why does this only alert 1 ? function test() { var myobj = {
Why the array is not overflowed (e.g. error alert) when the array is declared
i am using javascript for loop, to loop through a particular array and alert

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.