Development Tools Website Optimization Chrome DevTools Basics The Console API

Brian Haucke
Brian Haucke
13,717 Points

for(;;)

I'm not familiar with the loop "for(;;)" that Nick uses a couple times in this course. Could some one explain it to me?

1 Answer

for (/* optional deceleration */ ; /* optional condition */ ; /* optional increment */) {
    // Infinite loop.
}
while (true) {
    // Infinite loop, except that "while (true)" is more characters than "for(;;)".
}

An empty for loop is an infinite loop because no condition exists to terminate the loop. Although then he uses a conditional to break that loop. Furthermore in the ECMAScript 5 spec:

IterationStatement : for ( ExpressionNoIn ; Expression ; Expression ) Statement . . .

  1. If . . .
  2. Let . . .
  3. Repeat
    • If the first Expression is present, then
      • Let testExprRef be the result of evaluating the first Expression.
    • Let . . .

Which seemingly keeps repeating and never evaluating to false. He could have written:

function MyLogTest(MyVar) {
    console.time("Loop");
    for (MyVar = MyVar; MyVar != MyVar; MyVar++) {
        console.log("The value of MyVar is " + MyVar + ".");
    }
    console.timeEnd("Loop");
}