21

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;
Lenny Magico
  • 1,183
  • 5
  • 16
  • 28
  • I'm not really a javascript guy, that's why, no book, this is only because I'm doing something with google maps api and this requiers a bit of js knowledge which I don't have a lot of :) – Lenny Magico May 07 '11 at 19:11

5 Answers5

41

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;
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • the problem now is that bob1 is a variable which has a value further on. The code yu have given me creates a string, I need bob1 to be a variable name..., sorry, I asked the question slightly wrong, but is what I would like even possible? I know it's possible in php but is it javascript? – Lenny Magico May 07 '11 at 18:53
  • 1
    @Lenny: Using the generated string "bob1" as a variable name is a completely different and separate question. You _can_ do this using `eval`, but [it's generally frowned upon](http://stackoverflow.com/questions/592862/javascript-variable-variables/592944#592944). It might be best to rethink some aspects of your code, perhaps making use of arrays. If you have any queries regarding this, please post a new question as it _is_ a separate thing. – Lightness Races in Orbit May 07 '11 at 18:58
  • What I am doing is, I am calling position coridantes from a mysql database and I would like to use them on google maps, there is a work around and that is doing this seperate and creating each marker seprately but since this may be for over 100 markers on a google map it could cause problems, I have heard of eval but I am slightly confused on how to use it, but I'll I will have a look to see if I can find a useful tutorial :) – Lenny Magico May 07 '11 at 19:02
  • @Lenny: I've had similar issues with markers. That is of course _entirely_ unrelated to this question, but I will be interested to see any questions you post in the future regarding Google Maps markers. Please do consider using an array: with Javascript "associative arrays" (objects) you can retrieve properties/values through names that are themselves given by variables. – Lightness Races in Orbit May 07 '11 at 19:04
  • Brilliant eval works and does what I would like it to do, asking the question was difficult, that's why I apologies for causing so much confusion :) – Lenny Magico May 07 '11 at 19:06
  • the webpage I'm creating was really only there to test out the php preg_match_all function and to scrape some information of a website, but I got stuck with the idea of making an airplane tracker, and it seems to be on the way to working soon :) thanks for your help – Lenny Magico May 07 '11 at 19:09
10

ES6 introduce template strings for concatenation. Template Strings use back-ticks (``) rather than the single or double quotes we're used to with regular strings. A template string could thus be written as follows:

// Simple string substitution
let name = "Brendan";
console.log(`Yo, ${name}!`);

// => "Yo, Brendan!"

var a = 10;
var b = 10;
console.log(`JavaScript first appeared ${a+b} years ago. Crazy!`);

//=> JavaScript first appeared 20 years ago. Crazy!
Muneeb
  • 1,469
  • 16
  • 24
6

warning! this does not work with links.

var variable = 'variable', another = 'another';

['I would', 'like to'].join(' ') + ' a js ' + variable + ' together with ' + another + ' to create ' + [another, ...[variable].concat('name')].join(' ').concat('...');
Abdullah
  • 968
  • 12
  • 17
4

You can use the JavaScript String concat() Method,

var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2); //will return "Hello world!"

Its syntax is:

string.concat(string1, string2, ..., stringX)
K0m1sh
  • 43
  • 4
3

if you want to concatenate the string representation of the values of two variables, use the + sign :

var var1 = 1;
var var2 = "bob";
var var3 = var2 + var1;//=bob1

But if you want to keep the two in only one variable, but still be able to access them later, you could make an object container:

function Container(){
   this.variables = [];
}
Container.prototype.addVar = function(var){
   this.variables.push(var);
}
Container.prototype.toString = function(){
   var result = '';
   for(var i in this.variables)
       result += this.variables[i];
   return result;
}

var var1 = 1;
var var2 = "bob";
var container = new Container();
container.addVar(var2);
container.addVar(var1);
container.toString();// = bob1

the advantage is that you can get the string representation of the two variables, bit you can modify them later :

container.variables[0] = 3;
container.variables[1] = "tom";
container.toString();// = tom3
gion_13
  • 41,171
  • 10
  • 96
  • 108
  • 1
    You (correctly) declare the abstraction `addVar`, but then in the example you choose instead to use direct array access poorly! You also use `var` (which is a keyword) for variable names, then attempt to call `add` (which does not exist). Also, your `toString()` function returns a lot more than desired, because you iterate over _all_ properties of the `variables` object, not just those with numeric keys. After fixing these errors, the script runs and produces `"1bob"`, not `"bob1"`. Similarly, your second example gives `"3tom"`, not `"tom1"`. – Lightness Races in Orbit May 07 '11 at 22:44