Declaring two variables in a for loop

Is it possible to declare two variables in the initialization part of a for loop? I want to call a function on each character of a string.

for(var i = 0, c = aString.charAt(i); i < aString.length; i++){//problem here: not itterating alert("c: "+c) func1[typeOfChar(c)]++
}

The problem is the string isn't being itterated in the sense c is always the first letter of the string. The alert was just for trouble shooting purposes, by the way.

I'm curious, how come c doesn't need the var keyword when being declared?

UPDATE: got it working. I wasn't going to ask but I notice edits are still being made, I'm used to not using the semi-colons as they are optional. How can a for loop be written without them? I don't add them because I see it as the less the simpler, or do they improve readability?

6

7 Answers

You'd like c to change at every iteration, not to declare it at the start of the loop, try

var i,c;
for(i = 0,c=aString.charAt(0); i < aString.length; ++i, c = aString.charAt(i)){ alert("c: "+c) func1[typeOfChar(c)]++
}

For what it's worth I don't think it makes very readable code, I would put it in the first line.

Here is some information on the comma operator you're using.

Also note that javascript has no block scoping for for loops, so you're actually declaring i and c at the top of the current scope (this is usually the top of the current function, or the top of the global scope).

Here is a fiddle:

3

Simple way to include multiple incrementing variables in a for loop without nesting. This example declares 3 variables.

for (var i = 0, j = 1, n = 2; i < 50, n < 50; i = i + 3, j = j + 3, n = n + 3){ console.log("variable i: " + i); console.log("variable j: " + j); console.log("variable n: " + n);
}

see codepen here

In this case, because c is only dependent upon i (and an invariant) and it is not used the conditional of the loop, I recommend removing it from the loop construct:

// (Keep variable hoisting in mind)
for(var i = 0; i < aString.length; i++){ var c = aString.charAt(i); alert("c: "+c); // ..
};

(The problem with the original is that it never updated the value of c.)

You should just place c = aString.charAt(i); inside the body of the loop.

For example:

for(var i = 0; i < aString.length; i++){ c = aString.charAt(i); alert("c: "+c); func1[typeOfChar(c)]++
}

Yes, it is possible by using a multiple var statement, and you did successfully. However, assigning to it only once in the initialisation statement will not make it change.

You will either need to do it once before the loop and after each loop turn

for (var i=0, c=str.charAt(i); i<str.length; c=str.charAt(++i)) …

or you do it before each turn in the condition

for (var i=0, c; c=str.charAt(i), i<str.length; i++) … // comma operator
for (var i=0, c; c=str.charAt(i); i++) … // empty string as condition

or just move it inside the loop

for (var i=0, c; i<str.length; i++) { c=str.charAt(i); … }

Here:

c = aString.charAt(i)

i is always zero, so it's not going to work as expected. Initialization occurs once and you are trying to get the char during init when i has just been initialized to 0.

1

Another option is using while,in some case like that it's clearer:

var i = 0;
while(i < aString.length){ c = aString.charAt(i); alert("c: "+c) func1[typeOfChar(c)]++ i++;
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like