0

I've seen this in some JavaScript code but I don't get what it does.

for(;;){

  //other code

}

I'm used to the for(i=0;i<someLength;i++){...} format but I'm stuck to figure out how or what the "(;;)" syntax is for?

Dave
  • 3
  • 1

5 Answers5

6

In JavaScript, for (;;) { ... } just creates an infinite endless loop, which is almost exactly the same as:

while (true) {
    // ...
}

or

do {
    // ...
} while (true);
jhartz
  • 1,566
  • 9
  • 12
  • Ah, ok. So is this an "acceptable" use of for? Or would the while(true) be a more clear syntax? – Dave Feb 05 '11 at 03:17
  • `while(true)` would probably be more noticeable by other people and would probably show your point of creating an endless loop better. I'm not sure if there are any performance considerations, though (besides the fact that it will go on forever until ended). It might also be better to do something like: `var tester = true; while(tester) { /* ... */ tester = false; }`, which will break when you set tester to false (or you could just use the [break statement](https://developer.mozilla.org/en/JavaScript/Reference/Statements/break) instead). – jhartz Feb 05 '11 at 03:21
  • Tried both loops 1000000 times with Chrome and the difference was negligibly in favor of `for(;;)` ~1% faster. – fncomp Feb 05 '11 at 06:14
  • I used my simple bench function: http://stackoverflow.com/questions/1003855/howto-benchmark-javascript-code/4644364#4644364 – fncomp Feb 05 '11 at 06:22
  • The for and while loop syntax is inherited from C. For a long time, C had no built-in booleans, so the alternative to for(;;) was while(1). Both are acceptable C idioms, but not both acceptable in all C shops. When while(true) is available, that seems preferable on grounds of readability and conceptual clarity. Worrying about performance is a severe case of premature optimization; it's going to depend on the implementation which will differ between browsers and even versions of browsers. – Jim Balter Feb 08 '11 at 05:23
4

for (;;) is the exact same syntax. You're allowed to leave out parts that go between the semicolons, even all of them. If you leave the condition out, it's assumed to be true, so it results in an infinite loop. You can of course still exit with break or return or whatnot, hence making it not useless.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
3

It creates a loop that runs indefinitely. It's the same syntax you're used to seeing, there's just no code between the semicolons.

The syntax for a for loop includes an initializer, followed by a semicolon, followed by a condition for continuing the loop, followed by a semicolon, followed by code to run after each iteration of the loop.

Since there is no initializer, no condition that ever evaluates to false, and no post-loop code, the loop runs forever.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
1

That's an infinite loop. A for loop has 3 sections separated by ;: the initialization section where you normally declare and define your counter's starting conditions, the middle conditional statement that evaluates to true or false, and the increment section.

All 3 of these sections are optional. You can include any or none. The loop continues to loop until the conditional is false. If that condition is never met (since it was left out), it loops forever.

A place where you'll likely see it is prepended to JSON data as described by this SO post.

Community
  • 1
  • 1
Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
-1

always true https://developer.mozilla.org/en/JavaScript/Reference/Statements/for

Kris Ivanov
  • 10,476
  • 1
  • 24
  • 35