1

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?

asked Jul 6, 2011 at 17:28
2
  • Take a look at this : stackoverflow.com/questions/500431/javascript-variable-scope Commented Jul 6, 2011 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. Commented Jul 6, 2011 at 17:57

9 Answers 9

1
 for (var i = 0; i < numTimes; i++) {
 DoStuff();
}
function DoStuff() {
for (var i = 0; i < 100; i++) {
 console.log(i);
}
}
answered Jul 6, 2011 at 17:30
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Jul 6, 2011 at 17:33

Comments

0

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

for (var i = 0; i < 100; i++)
answered Jul 6, 2011 at 17:31

Comments

0

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

for (var i = 0; i < 3; i++) {
 console.log(i);
}
answered Jul 6, 2011 at 17:32

Comments

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

answered Jul 6, 2011 at 17:33

Comments

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++){
answered Jul 6, 2011 at 17:33

Comments

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

answered Jul 6, 2011 at 17:35

Comments

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.

answered Jul 6, 2011 at 17:37

Comments

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);
 }
}
answered Jul 6, 2011 at 17:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.