admin管理员组

文章数量:1336633

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 e 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?

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 e 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?

Share edited Mar 29, 2013 at 18:39 nneonneo 180k37 gold badges328 silver badges410 bronze badges asked Jun 20, 2012 at 4:34 timmackaytimmackay 1,0244 gold badges13 silver badges27 bronze badges 1
  • By advanced I mean encapsulation, recursion, closures, etc. Things I don't fully understand. Thanks. – timmackay Commented Jun 20, 2012 at 4:35
Add a ment  | 

5 Answers 5

Reset to default 4

It's OK and perfectly legal. Any of the three slots in a for statement can be left empty. Except for the condition that i added, it's logically the same as this:

i = 0;
while (true) {
    // other code here
    if (some condition) {
        break;
    }
    i++;
}

It will be an infinite loop unless there is a test somewhere in the loop that issues a break statement under some conditions to stop the loop (I added a sample test to my code example).

That is valid. Each of the three expressions in the for loop for(expr1; expr2; expr3) can be empty. For example for( ; ; ) is the same thing as while(true).

expr1 is an expression that is executed once right before the loop starts. You can also have a variable declaration statement here instead of an expression which is what you have in your code.
expr2 is an expression that is executed right before every iteration of the loop, and if the value of the expression is falsy the loop is done. If the expression is omitted it is considered truthy.
expr3 is an expression that is executed right after every iteration of the loop.

The statement you have is essentially a while(true) loop (which should contain a break statement somewhere within it, that counts the number of times it iterates using the variable i.

It is a valid approach to do an infinite loop with an incrementing counter. Whether that's intentional or not is a whole other matter, which is why I personally am against using for loops in odd cases like that. Comment would obviously fix any problems with such usage though

It's perfectly OK so long as inside the loop there's a break statement.

If there's no break statement, the JavaScript runtime will continue to execute the code in the loop, over and over again, with "i" growing and growing and growing. Soon, "i" will bee a number bigger than anything; bigger than the number of Legos that the rich kid you knew in school had in his playroom even. It'll go right past that to be a number bigger than the number of ants in the world, then the number of Starbucks, then the number of water molecules in the ocean. At some point the browser might ask if you want it to halt the script, but if you're curious, if you're the kind of person who likes to explore the unknown, then you'll decline the offer and let it go. Soon the value of "i" will reach truly astronomical values, and things will bee interesting. Remember how in 2001 A Space Odyssey things got weird when the dude got sucked into Jupiter or whatever? Well it might be like that.

Try a jsfiddle.

A loop has a simple syntax:

  1. Variable declaration => i=0
  2. Loop enterance check =>
  3. Creating another loop => i++

Because JavaScript returns true for nothing (correct me if I'm wrong), then the loop enterance check is always true.

This is not a good practice at all. Thus I strongly believe that this is a broken code. I haven't seen an example of an infinite loop by design ever.

本文标签: Javascript for loop missing middle part error or advancedStack Overflow