What does this syntax that loves `functionName: {}` mean?

I found a code snippet in my company project, for example:

while(condition){ code... reloop: { if(somethingIsTrue) { break reloop; } } code... } 

I don’t understand what reloop doing, can anyone give a simple explanation?

+5
source share
2 answers

reloop: is a label . They are rarely used and serve a very specific purpose: they allow you to break or continue outer loops from inner loops.

The MDN article on labels explains this better.

Note that shortcuts are very rarely used , and most of the time requiring a shortcut indicates that your code is unclear and needs to be restructured . I have never, ever used a label in javascript .

Avoid them unless they really are the only clean solution for a piece of code that is hard to write. Suppose instead, dividing the code into functions that you can return from.

+3
source

reloop is the label for the block. The break command break out of the marked block.

See for example https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break

+2
source

Source: https://habr.com/ru/post/1233069/


All Articles