Dealing with duplicate declaration warning for loop variables

前端 未结 5 1936
傲寒
傲寒 2021-02-02 05:44

Consider the following code:

for (var i=0; i<100; i++) {
    // your code here
}
// some other code here
for (var i=0; i<500; i++) {
    // custom code her         


        
5条回答
  •  半阙折子戏
    2021-02-02 06:43

    I tend the use the array built-in functions, such as map, filter and forEach, thus avoiding the issue altogether. This also automatically gives you scope loop bodies which is also quite useful.

    If using those doesn't match the use case I usually resort to top declaration, simply so I can avoid the issues you mentioned.

提交回复
热议问题