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 trialBrian Haucke
13,717 Pointsfor(;;)
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
moonshine
8,449 Pointsfor (/* 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 . . .
- If . . .
- Let . . .
- 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");
}