4

have the following javascript code

// note: declaring i in this loop
for( var i=0; i<args.length; i++ ) {

   var elem = args[i];
   ...

   if( elem.attr == 'class' ) {

        // note declaring arr and i in this loop
        for( var arr=elem.val.split(' '), i=0; i<arr.length; i++ ) {

            element.classList.add(arr[classCt]);
        }
        continue;
    }
}

the problem is that i in the second for loop is the same i as declared in the first for loop.

thought that the var construct allowed multiple variables to be declared separated by commas.

when changed i to classCt in second loop, the code worked as expected

2
  • 1
    "when changed i to classCt in second loop, the code worked as expected" - so why don't you just go with that? Commented Mar 18, 2012 at 1:53
  • Changed i to classCt where? The i in second for loop is indeed the same as the first because you are not initializing it again with var. Commented Mar 18, 2012 at 1:55

2 Answers 2

7

You only have one scope there, so there can only be one variable with the same name. You're correct that var allows multiple variables to be declared separated by commas, but you can't declare two different variables with the same name in the same scope. You're just redeclaring a variable that already exists.

Either change it to classCt, or do what I do and use the variable j (and so on) for nested loop iterators:

var i, j, k, l;
for(i = 0; i < 10; i++){
    for(j = 0; j < 10; j++){
        for(k = 0; k < 10; k++){
            for(l = 0; l < 10; l++){
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

you're right. a variable declared in a for loop is visible once the loop terminates, so the scope is not restricted to the for loop itself.
@ccyoung That's correct, Javascript is different some other languages such as C which use block scope. It uses function scope (every function has it's own scope and there is one global scope). Here's a link to read more: robertnyman.com/2008/10/09/…
@Niko - thought could create new scope with { } block - is this not true?
@ccyoung No, that is not true. As Paul explained, only functions create new scopes in javascript. This is important to know, as this is different in a lot of other languages like Java or C. See this fiddle for a demonstration: jsfiddle.net/5Dn7w
@Niko Technically, with also suffices in certain cases ... but that is not recommended :)
1

You are only working within one scope, the loop does not create it's own even if you use the var keyword. You are just overwriting your i variable within your current functional scope, so for example this:

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

Will just print 5,6,7,8,9.

If you want to create a new scope you would have to do it using functions as is typically done in javascript:

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

This will print 5,6,7,8,9 on their own lines 10 times.

1 Comment

but does using 'var' restrict the scope to the function, and if so, how to declare 2 variables within a for loop? :p

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.