timmackay
timmackay

Reputation: 1024

Javascript for loop missing middle part: error or advanced?

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?

Upvotes: 9

Views: 428

Answers (5)

jfriend00
jfriend00

Reputation: 707786

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).

Upvotes: 4

Paul
Paul

Reputation: 141887

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.

Upvotes: 3

Pointy
Pointy

Reputation: 413846

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 become 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 become 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.

Upvotes: 1

Saeed Neamati
Saeed Neamati

Reputation: 35842

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.

Upvotes: 0

Jani Hartikainen
Jani Hartikainen

Reputation: 43253

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

Upvotes: 1

Related Questions