0

I have some javascript code that resembles this:

for (i = 0; i < numTimes; i++) {
   DoStuff();
}


function DoStuff() {

for (i = 0; i < 100; i++) {

  console.log(i);

}

}

I am finding that the second time the DoStuff() is called, the value of i in the loop starts with 1. I assume this is due to the way the scoping of variables work in JS. Other than changing the variable name in the DoStuff() function, what's the cleanest way of resolving this and can someone explain this behavior?

EDIT: Thanks for the responses. It appears that JS has "lexical scope" instead of "block scope". Is this what I am seeing here? Can someone explain what lexical scope is in newbie terms?

Dusty
  • 2,159
  • 3
  • 23
  • 26
  • Take a look at this : http://stackoverflow.com/questions/500431/javascript-variable-scope – MD Sayem Ahmed Jul 06 '11 at 17:32
  • Since `i` already exists outside of the function, a better idea would probably be to just use a different iterator variable. What's happening is `i` is being overwritten in the second loop regardless of the presence of the `var` keyword. –  Jul 06 '11 at 17:57

9 Answers9

1
    for (var i = 0; i < numTimes; i++) {
   DoStuff();
}


function DoStuff() {

for (var i = 0; i < 100; i++) {

  console.log(i);

}

}
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
1

In javascript, any variable that isn't first declared with the var keyword is global. Adding the var keyword makes it function-local, that is local to the function. Javascript doesn't have block scoping, so if for instance you declared a variable with var inside and if block, it would not be local to the if block, it would be local to the function that contains it.

Davy8
  • 30,868
  • 25
  • 115
  • 173
0

Use the var keyword. This will limit the scope of i

for (var i = 0; i < 100; i++)
jglouie
  • 12,523
  • 6
  • 48
  • 65
0

Change i = 0; to var i = 0; Example:

for (var i = 0; i < numTimes; i++) {
    DoStuff();
}

function DoStuff() {
    for (var i = 0; i < 100; i++) {
        console.log(i);
    }
}
Shaz
  • 15,637
  • 3
  • 41
  • 59
0

Put a var in front of the variable inside the for loop:

for (var i = 0; i < 3; i++) {
    console.log(i);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0
for (var i = 0; i < numTimes; i++) {
   DoStuff();
}


function DoStuff() {

for (var i = 0; i < 100; i++) {

  console.log(i);

}

}

You should declare iterator variable with "var". If you do not, then you declare global scope variable

longchiwen
  • 822
  • 6
  • 11
0

your i variable is being implicitly set at a global level, meaning it is accessible (and modifiable!) to any script, anywhere. This is a bad thing, as you have just discovered. The solution is to use the var keyword, which limits the variable to the nearest enclosing function:

for(var i=0; i<100; i++){
0

To elaborate on a previous answer,

changing i = 0 to var i = 0 will give you the behavior you're looking for. The reason for this is that if you declare without the var you are declaring it as a global variable, whereas declaring with var makes it local within the scope of the function it is defined in. It should be also be noted that variables declared outside a function, with or without var will be global.

More info here

dave
  • 12,406
  • 10
  • 42
  • 59
0

If you don't declare your variable (i.e. using "var i;" or "var i=0;"), it is created as a global variable, and its scope is the whole program. (THIS IS VERY BAD!).

Note also that JavaScript does not have block scope, and so if you declare your variable in the for loop, it still has scope for the entire function.

TvC
  • 1