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)

4
  • 1
    possible duplicate of How to find JavaScript variable by its name Commented Jan 13, 2014 at 23:56
  • Why? This problem doesn't make much sense. Why do you need to do this? Commented Jan 13, 2014 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? Commented Jan 14, 2014 at 2:08
  • You are almost certainly doing this horribly wrong. You should never need to do something like this. You should use arrays instead. Commented Jan 14, 2014 at 2:09

4 Answers 4

1

You can do

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

which results in running the code

anawesomevariable = "new value";
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

If awesomevariable is a global variable you can do this:

window[variabletwo] = 'goodbye world';

Comments

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.

3 Comments

This is the correct way to do it, except that it doesn't help the situation given in the original question.
@JosephMyers: if the answer is eval, you're asking a wrong question.
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.
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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.