Dealing with duplicate declaration warning for loop variables

前端 未结 5 1873
傲寒
傲寒 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:20

    let keyword is introduced in javascript 1.7. Please find MDN documentation here

    Replacing var to let inside for loop solves the problem and now one can declare local variable inside the scope of a loop. Check out stackoverlow community explanation here: What's the difference between using "let" and "var" to declare a variable?

    Code from future:

    for (let i = 0; i < someVar.length; i++) {
        // do your thing here
    }
    

提交回复
热议问题