3

I am trying to use a variable as another variables name. For example, I have the following:

var dynamicname = 'name1_'

And I am trying to set it as the name of a new variable like this:

dynamicname + '2' = 'myvalue'

This obviously isn't working but is there a way to achieve this? Would eval be suitable?

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • why do you want to do this? – dustytrash Sep 09 '18 at 15:07
  • 3
    Possible duplicate of ["Variable" variables in Javascript?](https://stackoverflow.com/questions/5187530/variable-variables-in-javascript) – jonrsharpe Sep 09 '18 at 15:09
  • 3
    **Never** use `eval`. Never. Dynamic variable names, in general, are a bad practice. Try using `objects`; much easier and they support dynamic property names. – weirdpanda Sep 09 '18 at 15:10
  • Ok, looks like objects is the way to go then. Reading up on it now – fightstarr20 Sep 09 '18 at 15:15
  • @fightstarr20, I have posted an answer with regards to the `object` approach. :) – weirdpanda Sep 09 '18 at 15:18
  • What do you mean objects are the way to go? What is it you're trying to do? – dustytrash Sep 09 '18 at 15:19
  • @dustytrash, I am trying to set a variable every time a video is played so I can check back to see what videos have already been played. There are several videos on a page and they are dynamically created – fightstarr20 Sep 09 '18 at 15:24
  • 1
    @fightstarr20, if that's your use case, I would **strongly** suggest Objects since you can manage the meta properties of the videos with it. – weirdpanda Sep 09 '18 at 15:25

5 Answers5

3

To my knowledge, this is the best method.

window["varname"]=32;
console.log(varname)
UnJust1ce
  • 31
  • 1
2

Slightly off, but a possible answer to this using objects can be something along the lines of:

const obj = {
    [ dynamicNameVar + '2' ]: ...
}

Or, for the old-school folks,

var obj = {};
obj[dynamicNameVar + '2'] = ...

I hope this helps! :)

weirdpanda
  • 2,521
  • 1
  • 20
  • 31
1

As mentioned in the comments, you should never do this.

But if you want to do this:

var name ="name1_";

// declare a new var as name1_test and set it to 'TestWorked'.
eval("var "+name+"test = 'TestWorked'");

// prints value of name1_test ('TestWorked')
console.log(name1_test);
dustytrash
  • 1,568
  • 1
  • 10
  • 17
1

For just keeping an indicator/flag for a seen video, you could use Set or if you need more stored information, take a Map instead of using dynamically created variables where you still need to create an accessor for the variable as well.

var seen = new Set;

console.log(seen.has('foo')); // false;

seen.add('foo');

console.log(seen.has('foo')); //  true;
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

As others said, this is a bad practice and you should use objects. But if anyone really needs to do this for some reason, the right way to do is to use the window object, instead of eval. In your case it would be:

var dynamicname = 'name1_'
window[dynamicname + '2'] = 'myvalue'

You can do alert(name1_2) and it would show "myvalue".

Burhan B
  • 130
  • 8