-1

I have a set of variables:

var var1 = 0;
var var2 = 0;
var var3 = 0;
var var4 = 0;
var var5 = 0;

And I want to check them all with a for Loop, but I'm not quite sure of the syntax:

for( var i = 1; i<6; i++){
     alert(var[i]);
}

that for loop yields no results.

Murphy1976
  • 1,415
  • 8
  • 40
  • 88
  • 5
    why not have an array and do `array[i]` in loop? – Mritunjay Aug 27 '14 at 14:59
  • 2
    There's no good way to do that. You could use `window['var1']` etc. but that's just hinky. Use an array or object instead. – adeneo Aug 27 '14 at 15:00
  • Possible duplicate of [Javascript dynamic variable name](http://stackoverflow.com/questions/5117127/javascript-dynamic-variable-name) – Teemu Aug 27 '14 at 15:02

5 Answers5

2

If you're defining the varables in the global scope, you can access the values using window['var'+i]:

for(var i = 1; i<6; i++){
     alert(window['var'+i]);
}
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
2

Put them in an array instead.

var vars = [0, 0, 0, 0, 0, 0];
for(var i = 0; i < vars.length; i++) {
alert(vars[i]);
}
evilunix
  • 960
  • 6
  • 11
  • 1
    Why would this be downvoted? This is the proper approach and the best advice for a programmer new to JavaScript. – Pointy Aug 27 '14 at 15:01
  • this is a hack not actually an answer :) – Axel Aug 27 '14 at 15:01
  • This is changing the question. – user2908232 Aug 27 '14 at 15:01
  • 2
    @Axel how is it a "hack"? You're really not correct here - what the OP wants to do is the real "hack". – Pointy Aug 27 '14 at 15:02
  • @user2908232 No where it's changing question, I think it's the best way to overcome a problem like above – Mritunjay Aug 27 '14 at 15:02
  • @Axel totally agree with Pointy, no reason to downvote whatsoever. – evilunix Aug 27 '14 at 15:03
  • 1
    @Mritunjay: "And I want to check them all with a for Loop, but I'm not quite sure of the syntax:" The question is about the proper syntax to access the already given data. – user2908232 Aug 27 '14 at 15:03
  • 2
    @user2908232 and the correct answer is "your approach to solving your larger problem was fundamentally wrong" – Pointy Aug 27 '14 at 15:04
  • 1
    @Pointy: Except the part where you can't blindly start changing other code always. That's why you have to trust the question at least to some extend and comments are meant to ask for clarification. Did he mean the data was already defined like that? – user2908232 Aug 27 '14 at 15:05
  • i agree with @user2908232. of course nobody should advice somebody to do such things as a coding practice but everything depends on the specific circumstances. i guess we all had just seen too many "dump" questions :) – Axel Aug 27 '14 at 15:09
  • KIDS! Please, I've already voted this as the correct answer. Putting the variables in an array and looping through them that way is MUCH easier and best practice. @evilunix, kudos on ya! thanks for the answer. – Murphy1976 Aug 27 '14 at 15:23
2

To access them you would have to use the scope to which they were written. If your code is in the window scope that would then become:

for( var i = 1; i<6; i++){
     alert(window['var'+i]);
}

Though of course it's far cleaner if it's in a different scope specific to whatever you're doing. In those cases often

for( var i = 1; i<6; i++){
     alert(this['var'+i]);
}

would work.

user2908232
  • 441
  • 3
  • 12
1

The data construct you are using is not good for this. Using an array or object is much more feasible for what you want to do as well as being easily extendable.

var arr = [0,0,0,0,0,0];

for (var i = 0; i < arr.length; i++) {
    alert(arr[i]);
}
David Barker
  • 14,484
  • 3
  • 48
  • 77
  • this is a hack not actually an answer :) – Axel Aug 27 '14 at 15:02
  • This is changing the question. The data construct is a given of the question. – user2908232 Aug 27 '14 at 15:02
  • @user2908232 Stackoverflow is intended to provide guidance to people. Helping somebody implement a bad idea is not really "help". – Pointy Aug 27 '14 at 15:03
  • @user2908232 yes it is... for a good reason... pointing someone in the right direction on how you should organise data is usually far better than blindly answering the question with bad code. – David Barker Aug 27 '14 at 15:04
  • this is NOT a hack. changing the list of variables to an array is best practice. I have already chosen the first array solution as the correct, but since this is the exact same, this is also correct. @David Barker, thank you for your assistance. – Murphy1976 Aug 27 '14 at 15:25
0

var[i] is used it var is an array. But in your case it isn't. The best way would be to place those values in an array, like;

var myvar = [0,0,0,0,0];

then use the for loop to check for the value.

for( var i = 1;i<6; i++){
alert(myvar[i-1]);
}