Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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");
}