Javascript for loop missing middle part: bug or advanced?

I'm in the process of debugging another Javascript developer for a project at work.

I'm probably a mid-level Javascript developer on a nice day, and I came across a for loop that looks broken:

for(i = 0; ; i++) 

Can someone tell me if this is really a mistake, or if in some cases this is a perfectly acceptable way to do advanced things?

+8
javascript for-loop
source share
5 answers

This is normal and perfectly legal. Any of the three slots in the for statement can be left blank. Except for the condition that I added, it logically matches this:

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

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

+4
source share

It's right. Each of the three expressions in the for for(expr1; expr2; expr3) loop for(expr1; expr2; expr3) may be empty. For example, for( ; ; ) is the same as while(true) .

expr1 is an expression that is executed once before the start of the loop. You may also have a variable declaration instruction here, rather than an expression that you have in your code.
expr2 is an expression that runs immediately before each iteration of the loop, and if the value of the expression is false, the loop executes. If an expression is omitted, it is considered true.
expr3 is an expression that runs immediately after each iteration of the loop.

You have essentially a while(true) which should contain a break statement somewhere inside it) that counts the number of repetitions using the i variable.

+3
source share

This is normal if there is a break statement inside the loop.

If there is no break statement, the JavaScript runtime will continue to execute the code in a loop over and over, with the "i" growing, growing and growing. Soon, "i" will become more than anything; more than the number of legos that a rich child you knew at school, even in your playroom. This will go past being a number greater than the number of ants in the world, then the number of Starbucks, and then the number of water molecules in the ocean. At some point, the browser may ask if you want it to stop the script, but if you are wondering if you are the kind of person who likes to explore the unknown, then you will decline the offer and let it go. Soon the value of ā€œiā€ will reach truly astronomical values, and everything will become interesting. Remember how in 2001 A Space Odyssey all became weird when the dude got involved in Jupiter or something else? It may be so.

Try jsfiddle.

+1
source share

This is a valid approach for an infinite loop with an incremental counter. Whether this is intentional or not is another question, so I personally do not mind using for loops in such strange cases. The comment clearly fixes any problems with this use, although

+1
source share

The loop has a simple syntax:

  • Variable declaration => i = 0
  • Check for loop entry =>
  • Creating another loop => i ++

Since JavaScript returns true for nothing (correct me if I am wrong), then checking for loop input is always true .

This is not a good practice. Therefore, I firmly believe that this is broken code. I have never seen an example of an endless design loop.

0
source share

All Articles