Philipp Bergmann Philipp Bergmann - 5 months ago 10
jQuery Question

How do I create dynamic variable names inside a loop?

I'm working on an ajax google maps script and I need to create dynamic variable names in a for loop.

for (var i = 0; i < coords.length; ++i) {
var marker+i = "some stuff";
}


What I want to get is:
marker0
,
marker1
,
marker2
and so on.
and I guess there is something wrong with
marker+i


Firebug gives me this:
missing ; before statement

Answer

Use an array for this.

var markers = [];
for (var i = 0; i < coords.length; ++i) {
    markers[i] = "some stuff";
}