View Full Version : Using for loop to count through variables?

03-10-2004, 09:11 AM
I searched the forums and google and I kept ending up at irrelevant pages. Perhaps I wasn't searching for the correct term.

Imagine I have a set of variables (Item1 - Item10) and I want to do something with their values. (anything really). How can I set up a FOR LOOP that will count from 1-10, add that as "string" to the end of "item" and then do my thing. Yes this would be easier with arrays, but it has others uses, I just need to know how to do it. :D

The way I do it now (yikes)

Var item1 = "";
Var item2 = "";
Var item3 = "";


How can I do it like this?

For (X=1; X <= 10; X++;) {
Var item+x = "";

This is probably really easy. :p

03-10-2004, 09:23 AM
For (X=1; X <= 10; X++ {
window['item'+x] = "";

03-10-2004, 09:33 AM
Thank you, that does trick!

03-10-2004, 10:26 AM
More info on that trick here (http://www.litotes.demon.co.uk/js_info/sq_brackets.html).

03-10-2004, 05:49 PM
Thanks for that link. It's nice to know why something works instead of just using it not knowing what it does. Much appreciated.