DEV Community

loading...

Discussion on: JavaScript, stuff you've never heard of (probably): labeled loops?

Collapse
samthomson profile image
Sam Thomson

Hey, nice post.
I can't think when I'd use this. Usually if there are exit conditions I use a while loop. When would I use a named for loop and not a while loop containing a for loop - or merged together like this:

for (let i = 0; i < ar.length && !extrenousCondition; i++) { }

Are named for loops just stylistic or?

Collapse
adam_cyclones profile image
Adam Crockett Author
parent: for ( ... ) {
     child: for ( ... ) {
         if (exitConditionParent) break parent;
     }
}

You could do a labeled while loop as well.

I don't advocate or not advocate that anyone use this technique just that it's there and it's trivia.

Collapse
samthomson profile image
Sam Thomson

Yeah I see. Interesting either way, thanks.

Thread Thread
adam_cyclones profile image
Adam Crockett Author

Your most welcome 😁