Declaring two variables in a for loop

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: http://jsfiddle.net/maWua/

Leave a Comment