1

To begin with, I apologize if this question has been asked previously. I have searched for something similar to what I'm asking here and have found nothing but dead ends.

I am reviewing some older code for a project and I am using Brackets as my IDE with the JSLint extension. While going through my code it recommended that I change a line similar to

for(var i = 0; i < somevalue; i++)

to

var i;
for(i = 0; i < somevalue; i++)

This prompted me to ask; is there is any significant difference between the two declarations from a performance aspect, coding standard aspect, or etc.?

Thank you for any answers or leads!

Tyler
  • 129
  • 9
  • See the accepted answer here :) http://stackoverflow.com/questions/15783144/is-it-bad-practice-to-use-the-same-variable-name-in-multiple-for-loops – Vijay Jan 23 '15 at 14:49

3 Answers3

4

JSLint is actually asking you to move the variable declaration to the top of the function scope:

function myfunction() {
    var i,
        j,
        k;

    // other code

    for (i = 0; i < 100; i=i+1) { //another JSLint Recommendation: Don't use ++
        for (j = 0; j < 100; j=j+1) {
            for (k = 0; k < 100; k=k+1) {
                console.log(i);
                console.log(j);
                console.log(k);
            } 
        }
    }
}

The reason is that variables have function level scope in JavaScript. If I had declared 'j' inside of the for loop for i, it would have been 'hoisted' to the top of the function and would have actually existed throughout that whole function, not just in the for loop.

JSLint makes this recommendation because that's what is going on behind the scenes anyway, and you could be in for a rude surprise if you don't expect that to be the case.

Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • Thank you for the quick answer. It's a very detailed explanation however I do have to ask: is there any significant performance difference between declaring them at a function level as opposed to just the `for` loop, knowing that I will only need the variable for the single loop? – Tyler Jan 23 '15 at 14:46
  • @quandary1011101 There's no performance difference; but there may be a difference in bugs if you don't expect this to be the case. – George Stocker Jan 23 '15 at 14:48
2

Consider following 2 functions

function test1(){
    //code block 1
    for(var i = 0; i < somevalue; i++)
    //code block 2
}

function test2(){
    //code block 1
    var i;
    for(i = 0; i < somevalue; i++)
    //code block 2
}

In both cases the definitions are hoisted and the compiler first defines the variables. The compiler rearranges the code like this,

function test(){
    var i;  //and other definitions
    //code block 1(without definitions)
    for(i = 0; i < somevalue; i++)
    //code block 2(without definitions)
}

Therefore there is no difference..

Sampath Liyanage
  • 4,776
  • 2
  • 28
  • 40
  • 1
    There isn't a difference to the compiler; but to the human behind the keyboard there is a difference; if they're not expecting this to be the case, it could be a source of bugs. – George Stocker Jan 23 '15 at 14:46
0

var i binds the variable i to the local context. If you don't do var i, the variable is defined globally.

you should always use var in only one place, at least this is the best practice. Also you should use it per variable only once

so

for(var i = 0;i<10;i++)

is fine but

for(var i = 0;i<10;i++)
for(var i = 0;i<10;i++)

is not good better would be

for(var i = 0;i<10;i++)
for(i = 0;i<10;i++)

but if you do

 var i;
 for(i = 0;i<10;i++)

or not is totaly up to you

Spectator
  • 79
  • 6