Dynamic variable declarations?


I want to be able to access variables by using strings. for example.

for (var x = 0; x < 5; x++) {
 "test" + x = 500;

would assign 500 to variables test0, test1, test2, test3, and test4. is it apossible somehow to accomplish this? perhaps using a library. Or maybe this code would actually work haha don’t think so but never tried.

thanks, geoff

In this particular example if test were an array and x and index into the array you would have the same thing.

No - “test” + x = 500 will not work.
However - eval(“test” + x + " = 500") will.

for (var x = 0; x < 5; x++) {
 window["test" + x] = 500;

That is almost always never a good solution to the problem. Why can you not use an array to store the info?