3

I recently started using PHP Storm (love it) and noticed that it flags declarations of "var i" in for loops as duplicate in JavaScript. I learned that apparently the scope of that variable exists outside of the loop.

for (var i = 0; i < 10; i++) {
    // Do anything
}
console.log(i); // i == 10

When I do my next for loop, should I declare var i again? Or, should I just say i = 0? I know I can do either, but one seems like bad style, the other like bad implementation.

On one hand, you shouldn't re-declare a variable that's in scope, but if I, for instance, delete the first for loop that declares "i", then everything else will break.

Captain Stack
  • 3,102
  • 5
  • 25
  • 51

2 Answers2

3

JavaScript has only function level scope, no block level scope. So, if a variable is declared anywhere within a function, it will be available for the entire function. So, you don't have to declare it again.

The best practice is to declare all the variables used in the function at the beginning of the function.

For example,

function myFunction() {
    console.log(i);
    for (var i = 0; i < 10; i++);
    console.log(i);
}

myFunction();

Would print,

undefined
10

i is declared in the function, but till the for loop is executed, i is not assigned any value. So, it will have the default value undefined in it.

thefourtheye
  • 206,604
  • 43
  • 412
  • 459
  • Additionally, all the variables declared as function parameters have the exact same scope of locally declared variables. This may seem obvious, but maybe not so much for a beginner. – MaxArt Oct 03 '14 at 07:00
  • Right, I get that. My question is more about style. For instance, if I make a for loop and declare i, and then don't declare i in a later for loop, but delete the first for loop, nothing would work anymore. That said, it feels wrong to re-declare an in-scope variable over and over again. – Captain Stack Oct 03 '14 at 07:13
  • @CaptainStack I would recommend running your code through jslint or jshint, whenever you make changes to the code. – thefourtheye Oct 03 '14 at 11:24
1

you need not declare it again. you can simply reassign the value of i for the next loop like

              for (i = 0; i < 5; i++)
                {
             // Do anything
                  }
Sarath Kn
  • 2,394
  • 14
  • 24
  • Right, I get that I don't need to, but if I delete the prior for loop, the later ones would be broken. Given that i really only matters in the scope of the loop, it seems better to re-declare it, but it also feels wrong to constantly re-declare an in-scope variable. – Captain Stack Oct 03 '14 at 07:17
  • Yeah.. but also note one thing that it is not a must to declare the javascript variables before its use. You can even use 'i' without decalring 'var i' before. – Sarath Kn Oct 03 '14 at 09:30
  • Interesting. So what's the var keyword even for? – Captain Stack Oct 03 '14 at 21:59
  • @CaptainStack `var` is for declaring non-global variables. If `x` hasn't been declared in the current function, `var x = 0` makes a new variable local to the current function, whereas `x = 0` sets the global value of `x`, creating a new variable if necessary. Best practice is to make variables local (with `var`) unless they *need* to be global. – Gordon Gustafson Jan 30 '15 at 21:02