Why use an empty for loop in Javascript--for(;;)?

javascript, performance, syntax

Solution

It's shorter to type. That's really the only reason. A lot of people just grew up programming that way and it's a matter of habit.

I vaguely remember there being a time and language where `for(;;)` was faster. But that's by and large in the past now with any half-decent optimizing compiler/interpreter.

Problem

I forget where now, but I read somewhere that `for(;;){...}` is the same as saying `while(1){...}`. This answer confirms that this is true and explains why `for(;;)` works at all. However, it doesn't elaborate on why it would be useful, and I can't seem to find any benchmarks for the empty `for` loop as opposed to the `while` loop. It's rather confusing (although admittedly quite clever), but it is shorter. Google's Closure Compiler converts `while(true)` to empty for loops, so there must be a reason. tl;dr: Why should I use an empty for loop over a `while(1)` loop?

Original source

Related problems