I don't think any of them are good practice. In addition to that they make the code longer.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
Optional initialization
var i = 0;
for (; i < 9; i++) {
console.log(i);
// more statements
}
Disadvantages:
- Can have side effects
Optional condition
for (let i = 0;; i++) {
console.log(i);
if (i > 3) break;
// more statements
}
Disadvantages
- The only use case I can think of is when you are using this construct is when you are programming a microcontroller that waits for an input. Other than that, I simply don't know what you gain by pulling the condition inside the for-loop.
Optional final-check
for (let i = 0;i < 3;) {
console.log(i);
i++;
// more statements
}
Disadvantages
- This is so useless. I don't know what to think of it
All expression optional
var i = 0;
for (;;) {
if (i > 3) break;
console.log(i);
i++;
}
Disadvantages
- This combines all the disadvantages from above
I fail to see the benefit of using optional expressions in for-loops. But maybe I'm missing something? Could someone provide examples where the optional for-loop statement is superior to the standard for-loop?
for(;;) {
orwhile(1){
. For certain looping patterns (e.g. prompt first then ask for input, retry if necessary) it seems to reduce the cognitive load to allow the exit condition to be stated inside the loop and where/when it naturally occurs rather than trying to shoehorn into a while (or for-loop) using a non-vacuous condition.