0

I've got a bit of a problem here. I've got the following code declaring two variables:

var anawesomevariable = "hello world";
var variabletwo = "anawesomevariable";

As you can see, the second variable's contents are the same as the name of the first variable. My problem: I want to change the first variable using the contents of variabletwo. So in other words, I want to say "Hey Javascript, change the contents of the variable whose name is in variabletwo". Is there any way to do this in Javascript?

P.S. I havn't really explained that clearly, but you get my point (I hope)

Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
  • 1
    possible duplicate of [How to find JavaScript variable by its name](http://stackoverflow.com/questions/724857/how-to-find-javascript-variable-by-its-name) – Josh Mein Jan 13 '14 at 23:56
  • Why? This problem doesn't make much sense. Why do you need to do this? – tckmn Jan 13 '14 at 23:59
  • @DoorknobofSnow My example here doesn't make any sense, I'll admit that, **but** in the web app I'm making there are 10 buttons that all open the same pop-up window where the user can change stuff, and each of these buttons passes it's number to the function (button 1 passes 1, button 2 passes 2, etc). When the user clicks a button in the window, it needs to change a variable which includes the buttons number on the end. So I needed to access that variable using another variable (i.e. the actual number with the other text in front). Does that make more sense now? – Toastrackenigma Jan 14 '14 at 02:08
  • You are almost certainly doing this horribly wrong. You should *never* need to do something like this. You should use arrays instead. – tckmn Jan 14 '14 at 02:09

4 Answers4

1

You can do

eval(variabletwo + ' = "new value"');

which results in running the code

anawesomevariable = "new value";
Joseph Myers
  • 6,434
  • 27
  • 36
  • I don't know why this was downvoted. Clearly `eval` is the way to go here (unless of course the input is user-specified, or other evilness). Although I do say the OP's problem doesn't make much sense. – tckmn Jan 13 '14 at 23:57
1

If awesomevariable is a global variable you can do this:

window[variabletwo] = 'goodbye world';
Ezequiel Muns
  • 7,492
  • 33
  • 57
0

You cannot* (and should not) manipulate variables, but it's a piece of cake with properties:

var obj = {anawesomevariable: "hello world"};
var variabletwo = "anawesomevariable";

obj[variabletwo] = whatever

^* don't even think about "eval" here. seriously.

georg
  • 211,518
  • 52
  • 313
  • 390
  • This is the correct way to do it, except that it doesn't help the situation given in the original question. – Joseph Myers Jan 13 '14 at 23:57
  • @JosephMyers: if the answer is eval, you're asking a wrong question. – georg Jan 14 '14 at 00:02
  • You're almost always right because eval is almost always wrong. However, "Programs that write programs are the happiest programs in the world." Like root, eval is to be avoided at almost any cost, and *definitely* to be avoided in a simple case like this one. But because the writers of the language itself are too limited to anticipate and design for all possible uses of the language that they themselves designed, there are things like eval. Many programming problems can only be solved using code that writes code, although this isn't one of them. – Joseph Myers Jan 14 '14 at 00:15
0

You have a few options. Option 1, which uses eval(), which I discourage, would be simplest, like this

eval(variabletwo + ' = "cool"');

The second option is to declare them as globals, like this

window.anawesomevariable = "hello world";
window.variabletwo = "anawesomevariable"

and then

window[variabletwo]="foo";

However, if you want to keep something in the current scope, declare it in an object, like this

var obj = {anawesomevariable: "hello world"};
var variabletwo = "anawesomevariable";

obj[variabletwo] = "foo";
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64