admin管理员组

文章数量:1315333

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

for (;;) {
}

What does this even do? Can anyone answer?

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

for (;;) {
}

What does this even do? Can anyone answer?

Share Improve this question edited May 30, 2013 at 18:08 user1720624 asked May 30, 2013 at 17:55 picardopicardo 24.9k33 gold badges108 silver badges156 bronze badges 2
  • 4 Runs a for loop for ever and ever :) – tymeJV Commented May 30, 2013 at 17:57
  • 1 see the relevant section of ECMAScript specification. – zzzzBov Commented May 30, 2013 at 18:15
Add a ment  | 

6 Answers 6

Reset to default 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

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

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.

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.

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)

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 mand evaluates to true.

本文标签: How does this quotfor ()quot syntax work in JavascriptStack Overflow