scope of loop variables
-
What is better:
for (var i = 0; i < 200; i++){
or
for (i = 0; i < 200; i++){
and why i cant use:
for (local i = 0; i < 200; i++){What is the scope of "i" on that bucles?
Thanks -
I think I am following standard Javascript behaviour here which makes the scope global. There‘s no difference between
for(i=0
andfor(var i=0
), in fact this is the only case where I allowed the parser to accept unqualified assignments of variables.My recommendation is to define one
reg
variable called i in the onInit callback and reuse it (that‘s what I am doing when I need to iterate using this loop). Bear in mind that the other for loop we discussed yesterday is much more faster and should be preferred if possible. -
@christoph-hart thanks a lot