0

I have a few for loops in a document, a simplified example of their usage would be:

for (var i=0; i < element.length; i++) {
   element[i];
}

I read somewhere it is best to use different variable names for different loop instances i.e. instead of i use j and so on.

Because I have quite a few loops in the script though, it starts to become a lot harder to read with different letters when going through stuff.

If the variable is within parenthesis, will this be OK to still use i for all of them? I do not have any global variables called i.

I can't use let because my boss wants to keep it at var and not use babel etc.

Thanks in advance for any advice.

Em

pjk_ok
  • 618
  • 7
  • 35
  • 90
  • `I can't use let because my boss wants to keep it at var and not use babel etc.` This does not make any sense. You really should use ES6+ and transpile later (and use separate variables), it makes coding a whole lot easier. – CertainPerformance Oct 13 '18 at 15:21
  • 1
    @CertainPerformance sometimes it makes sense as babel, webpack, polyfills etc. are increasing bundle size significantly which could be unlikely for applications that are often used by mobile devices. – Eugene Tsakh Oct 13 '18 at 15:23
  • 2
    Use different letters when loops are nested. If you have 300 loops at the same indentation, you should use "i" in all of them – Héctor Oct 13 '18 at 15:23

3 Answers3

3

You can use the same variable for different loops if they are not nested, it's completely normal as your loops will be executed synchronously i.e. one after another so there will be no conflicts between them.

Eugene Tsakh
  • 2,777
  • 2
  • 14
  • 27
1

The variables will not be locally scoped if you use var. So depending on how you initialize the for you might get yourself in trouble. But if I may let's take a step back...

If you are doing multiple for loops in the same file aren't we violating the DRY principle (Do not Repeat Yourself)? Is there any way we can use a built in function like forEach or map? Or can we abstract what those loops are doing into our own custom function?

I think it'll be useful to explore that. But the short answer is you might run into problems depending on how you initialize the i variable in your for loops.

Clean code is important. It makes our lives easier!

Hope that helps!

miguelsolano
  • 519
  • 3
  • 11
  • Author said that he can't use ES6 features. – Eugene Tsakh Oct 13 '18 at 15:25
  • forEach is part of the Ecmascript 5.1 spec released in 2011. So compatibility for it is common among all browsers without Babel. – miguelsolano Oct 13 '18 at 15:28
  • Had to check for map but it's actually introduced in the minor version before that (5.0). It's even supported in IE9. You might be confusing these fuctions with optimizations made to forEach in a newer version found in ES6 and the Map data structure introduced in ES6. Be useful to read up on both of those. Cheers. – miguelsolano Oct 13 '18 at 15:31
0

If you are looping arrays , use .map instead of forEach . Read an article of why I recommend this https://codeburst.io/javascript-map-vs-foreach-f38111822c0f?gi=8024d1c18f31