I am in the process of debugging another developer’s Javascript for a project at work.
I am probably a mid-level Javascript developer on a good day and I’ve come across a for loop that appears broken:
for(i = 0; ; i++)
Can anyone tell me if this is indeed a mistake or if in some instances this is a perfectly valid way to do Advanced things?
It’s OK and perfectly legal. Any of the three slots in a
forstatement can be left empty. Except for the condition that i added, it’s logically the same as this:It will be an infinite loop unless there is a test somewhere in the loop that issues a
breakstatement under some conditions to stop the loop (I added a sample test to my code example).