As in if I have var x = "foo"
and I need to create a new variable called foo
by using x
. Like var "valueOf(x)" =
... or something. I can't seem to figure it out.
Asked
Active
Viewed 59 times
-2

Caio Felipe Pereira
- 805
- 11
- 14

Ferus
- 1,080
- 3
- 12
- 17
-
2What are you trying to accomplish with this? – forgivenson Jun 17 '15 at 13:10
-
1If I understand your questions, it seems to be a duplicate: http://stackoverflow.com/questions/5613834/convert-string-to-variable-name-in-javascript - hope this helps! – André Jun 17 '15 at 13:10
-
@Jackson — The question is asking how to use the value of a variable as the name of another variable, not how to give two variables the same value. – Quentin Jun 17 '15 at 13:10
-
What Im trying to accomplish is this: I have a variable that's is set to name and then another variable set to its value, I want to combine these two, the name with the value as in: var name = value – Ferus Jun 17 '15 at 13:14
-
@Ferus — Yes, that's what the duplicate question explains how to do. – Quentin Jun 17 '15 at 13:16
-
Alright, didn't see that post, thanks. – Ferus Jun 17 '15 at 13:21
2 Answers
0
What scope are these variables in? If they are on the window (global context ) you can use the bracket notation call window[x] = bar
and will now have a bar variable available.
If it's in a function it would be more clear to use this
instead of window

PhilVarg
- 4,762
- 2
- 19
- 37
-
In that case use `this[x] = 'bar'` and now the local variable foo will be available in that function and equal to bar – PhilVarg Jun 17 '15 at 13:22