Lets say I have an array of names for a variable:
var varNames = new Array("name1","name2","name3");
How do I create var name1
, var name2
and var name3
by just looping through the varNames
array?
Lets say I have an array of names for a variable:
How do I create |
|||||
|
This will create global variables (in the global namespace, i.e.
Since using global variables is considered bad practice, you could create variables within a custum object:
|
|||
|
The direct answer to your question would be - you can do it using
Please note though this is considered bad practice and usually there is no justification for using eval for such case. Also note that it's more common to create array using following style:
|
|||
|
You can do it as follows. I added the alerts to prove you can set a value to those variables.
|
|||
|