Dynamic variable name in loop

Solution 1:

Use an array:

var marker = [];

for (i=0; i < location.length; i++) {
   marker[counter] = new google.maps.Marker({

Solution 2:

Well, "Use an array" is undoubtably the right answer here, however if you really want dynamic variables you need to decide the scope of them - the default would be window and then you could do this:

var counter = 0;
for(i=0; i<location.length; i++) {
   ...
   window["marker_" + counter] = new google.maps.Marker({

This can now be accessed with the same square bracket notation

window["marker_0"]...

or the dot notation

window.marker_0