Lenny Magico Lenny Magico - 3 months ago 8
Javascript Question

How do I combine 2 javascript variables into a string

I would like to join a js variable together with another to create another variable name... so it would be look like;

for (i=1;i<=2;i++){
var marker = new google.maps.Marker({
position:"myLatlng"+i,
map: map,
title:"title"+i,
icon: "image"+i
});
}


and later on I have

myLatlng1=xxxxx;
myLatlng2=xxxxx;

Answer

Use the concatenation operator +, and the fact that numeric types will convert automatically into strings:

var a = 1;
var b = "bob";
var c = b + a;