Is it bad practice to use the same variable name in multiple for-loops?

后端 未结 6 1276
一个人的身影
一个人的身影 2021-02-01 12:20

I was just linting some JavaScript code using JSHint. In the code I have two for-loops both used like this:

for (var i = 0; i < somevalue; i++) { ... }
         


        
6条回答
  •  礼貌的吻别
    2021-02-01 12:54

    The reason JSHint shows the error is because in JS variable scope is function and variable declarations are hoisted to the top of the function.

    In Firefox you can use let keyword to define block scope, but is not currently supported by other browsers.

    The let keyword is included ECMAScript 6 specification.

提交回复
热议问题