Instantiate a variable whose name is the value of another

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Instantiate a variable whose name is the value of another

Post by kendall »

Can u create a variable whose name is the value of another in JS?

e.g.

Code: Select all

var variable1 = "myName";
// create the var named myName;
eval(variable) = 2 //...?
P.S. what is the tag used to highlight this as a javascript code?

Kendall
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

var varname = 'someThing';
eval(varname + ' = 2;');
I believe.. I avoid eval() at all costs.

Code: Select all

..
Post Reply