DEV Community

Panda Quests
Panda Quests

Posted on

What are the use cases of using optional for loop statements?

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

I fail to see the benefit of using optional statements 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?

Top comments (2)

Collapse
 
jonhaddow profile image
Jon Haddow

Never done this before, but I could imagine a scenario where I'd want to break out of a loop half way through the iteration, if a condition is met. Then if that was the only way to break out, then omitting the condition makes sense to me.

Collapse
 
pandaquests profile image
Panda Quests

Then just use an infinite loop, e g. While (true) { check condition }