4

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.

1
  • 1
    It is enough to just set i = 0 , no need to re-declare the variable. Commented Oct 3, 2014 at 6:58

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
@CaptainStack I would recommend running your code through jslint or jshint, whenever you make changes to the code.
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
                  }

4 Comments

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.
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.
Interesting. So what's the var keyword even for?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.