-1

My goal is to name a new variable with the content/string of a different variable.

function foo(id) {
    var example + id; // So basically add the id to the example variable making something like example1 etc...
}

foo(1)

example1 = "text"
console.log(example1) // And it logs "text"

Hope that explains it and that you can give a good response thanks!

undefinedChar
  • 537
  • 1
  • 5
  • 18
  • 1
    too lazy to look for the dupe.... Use an Object or an Array.... do not create more variables – epascarello Jan 04 '19 at 14:50
  • Any time you find yourself wanting variables dynamically named "something1", "something2", "something3", and so on then what you really want is called an "array". – David Jan 04 '19 at 14:53

1 Answers1

0

You can use an associative array:

var something=[];
something["example" + 1] = "hello"

console.log(something["example1"]) => "hello"
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176