Safe local variable in loop

Sorry for the poor question

function foo() {

   var a = [1,2];
   //var item="" (should i declare it?)

   loop array=a index="item" {
   }

}

To avoid conflicts with other variables, also “item” should be declared first with “var” or use “local.item”?

Many thanks and happy holiday! :sunny:

1 Like

either works, or just add function foo() localmode=true { ... }

I tend to like local.item as it makes it clear during code reviews, especially in large functions

2 Likes

fwiw, here’s another approach where you can skip the declaration:

for (local.item in a) {

}
2 Likes

I usually follow this - declaring the “index” variable as part of the FOR.

1 Like