3

I came across a strange syntax for the for in an open source Javascript library.

for (;;) {
}

What does this even do? Can anyone answer?

picardo
  • 24,530
  • 33
  • 104
  • 151

6 Answers6

10

It's "loop forever" and it works the same in C, C++, C#, Java etc. You've got a for statement with

  • no init step
  • no termination check (=> continue forever)
  • no end-of-loop step
Rup
  • 33,765
  • 9
  • 83
  • 112
  • You have nice answers: short and precise. May the upvotes and badges be with you. And thanks for that link to the java bouncy castle example, which I stole and turned into an answer ;-) – GhostCat May 09 '19 at 06:02
4

It's an infinite loop, just like while(true).

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
3

As others have already pointed out, if will run forever, but you can still end the loop with break. Like:

for (;;) {
    if (condition) {
        break;
    }
}

Mozilla has a good breakdown of how javascript evaluates for loops:

for ([initialization]; [condition]; [final-expression])
   statement

initialization
An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword. These variables are not local to the loop, i.e. they are in the same scope the for loop is in. The result of this expression is discarded.

condition
An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

final-expression
An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to update or increment the counter variable.

statement
A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.

Most relevant to this question is in the condition section:

This conditional test is optional. If omitted, the condition always evaluates to true.

2

A simple/empty ; in many languages acts as a blank but true (and executable) statement.

Hence, in the for loop, all three expressions are true, always matched and hence, results in an infinite loop.


It has also been on Wikipedia. As per the wiki page

This style is used instead of infinite while(1) loops to avoid a warning in Visual C++.


The controlling expression of an if statement or while loop evaluates to a constant. If the controlling expression of a while loop is a constant because the loop will exit in the middle, consider replacing the while loop with a for loop. You can omit the initialization, termination test and loop increment of a for loop, which causes the loop to be infinite (like while(1)) and you can exit the loop from the body of the for statement.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
2

This is valid for loop for (;;) {} if condition is not given in for loop its by default true. So loop run infinitely. same as for (;1;) {}

Additionally not only in Javascript its true in C, C# language also etc.

Read this: Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it? to know why some programmers may prefer for (;;) {} over simple while(true){} (interesting)

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
0

It runs an infinite loops, because

Essentially, a for loop is a shortened version of another loop, with this syntax

for(a;b;c){
}

is

a;
while(b){

c;
}

so loops forever, since the b is empty, and it has no increment operator to change anything. Basically, it's an infinite loop of nothingness. EDIT: Apparently, the empty command evaluates to true.

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
  • this isn't quite right. If the `for` loop ran as the `while` loop does in your example, none of the contents would ever be executed. `b` would be `undefined` which would be falsey, and the body would never execute. It's a quirk of the language that allows `for (;;)` to execute similar to a `while(true)` loop. – zzzzBov May 30 '13 at 18:10
  • Also, I assume you meant to write `for(a;b;c)` instead of with commas. – zzzzBov May 30 '13 at 18:11