tags:

views:

771

answers:

11

Duplicate:

Javascript Variable Variables

How can I create variable variable names in Javascript?

EDIT: I've clarified what I'm trying to accomplish here.

+1  A: 

Completely guessing what you mean, but you can use eval() to run arbitrary JS code. Be careful about included user generated material in here or you're exposing yourself to XSS attacks.

E.g

var myVarName = "abcd"; eval(myVarName + "=1"); alert(abcd);

Greg
+1  A: 

this?

eval( 'myvar' + idx )

or better

window[ 'myvar' + idx ]
MikeW
+7  A: 
js>x = {};
[object Object]
js>varname = 'foo';
foo
js>x[varname] = 3
3
js>x.foo
3

If you're looking for a variable belonging to the global namespace, rather than an object field, use "this" instead of "x" (but make sure you do it in a context where "this" refers to the global object). Or use "window" instead of x in a browser context.

If you're talking about a local variable within a function, I don't think there's a way to do that. (edit: except via "eval")

Jason S
+4  A: 

Do you mean something like this:-

var name = "test"
var value = "Hello World"

var o = {}
o[name] = value

alert(o.test);

Note unless you want to create global variables and you code is running with the global object at its this context you can't actually create independent variables, however the above approach is better anyway.

AnthonyWJones
+1 waaaaaaaaaay safer than eval
annakata
+2  A: 

I don't think it's possible. You can have variable property names though:

var newname = 'propname', obj = {};
obj[newname] = 'somevalue';
KooiInc
A: 

If you mean totally client-side, then eval might do it for you:

var varName = "myVar";
eval ("var " + varName + "=2;");
alert(myVar);

Generally though, server side scripting is normally used for generating client side script, if the script needs to handle such things as variables of dynamic names.

baretta
+5  A: 

Once you have the name of the variable, you can attempt to access it as a property of another object; otherwise, you will need to use eval.

var variableName = "foo";

// if the variable is global
window[variableName];

// if the variable is a property of an object
someObject[variableName]

// if the variable is in scope, but not global
eval(variableName);
Alex Barrett
Eval is a comparative resource hog, as well as a possible security hole. Beware.
Steve Goodman
+1  A: 

eval() function is your answer

var test_var = 'Hello'; 
var var_name = 'test_var'; // Set var_name to equal the name of our test variable
eval(var_name+"='Goodbye'"); // "eval()" some code that changes the value in our test variable
alert(test_var);
Oscar Cabrero
+2  A: 

Use the this object that always refers to the current scope:

var foo = "bar";
var bar = "baz";
alert(this[foo]);  // alerts "baz"
Gumbo
+2  A: 

Like the other examples it would be fine, although if you want global scope you would have to index to the window object.

var a = "hello";
window[a] = "world";
alert(a + " " + hello);

Produces: "Hello World"

Also accessible via window[a] or window["hello"] Like:

alert(a + " " + window[a]);
alert(a + " " + window["hello"]);

You can try it yourself..

<html>
    <head>
        <script type="text/javascript">
            var a = "hello";
            window[a] = "world";
            alert(a + " " + hello);
            alert(a + " " + window[a]);
            alert(a + " " + window["hello"]);
        </script>
    </head>
    <body>
    </body>
</html>
Quintin Robinson
A: 

What you are asking for is not possible, and not desirable. Once you have created a variable, it's name stays the same.

If you mean that you want to create variable names dynamically, that can be done using the eval function, but I encourage you not to use that. There is almost always a better method to accomplish what you are trying to do, so you should rather ask about how to solve that instead of asking about what you think is the solution.

Guffa