Reputation: 6882
I have found a piece of code in my company's project like the following:
while(condition){
code...
reloop: {
if(somethingIsTrue) {
break reloop;
}
}
code...
}
I don't understand what reloop
does, can anyone give a simple explanation?
Upvotes: 5
Views: 73
Reputation: 76899
reloop:
is a label
. They are rarely used, and serve a very specific purpose: they let you break
or continue
outer loops from inner loops.
The article on MDN about labels explains this better.
Note that labels are very rarely used, and most of the time needing a label hints that your code is unclear, and should be restructured. I have never, not even once, used a label in javascript
.
Avoid them unless they are truly the only clean solution to piece of code that proves difficult to write. Prefer, instead, splitting code into functions that you can return
from.
Upvotes: 3
Reputation: 6732
reloop
is a label for the block. The break
command breaks out of the labelled block.
See eg https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break
Upvotes: 2