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?
You’d like
cto change at every iteration, not to declare it at the start of the loop, tryFor 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
iandcat 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/