Is there any benefit of doing so and can it lead to readability concerns?
-
why negative vote? is there anything wrong in the question? – ripu1581 Mar 25 '13 at 04:21
-
(most likely because this kind of question doesn't fit SO format of 'ask a specific question, get a specific answer.' Questions that ask about vague, general questions tend to be down-voted and closed.) – Jeremy J Starcher Mar 25 '13 at 04:24
-
How is "Does break label in javascript lead to readability concerns?" a vague question? – ripu1581 Mar 25 '13 at 04:26
-
Take a look at the FAQ -- at the top of the document -- "You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page." – Jeremy J Starcher Mar 25 '13 at 04:28
3 Answers
While I don't believe in too many absolutes in coding, I will say that it is rare that I'd use a label in Javascript.
In fact, I don't believe I've ever used them at all the language.
Thanks to the powerful structures given, for ...
for ... in
while
do ... while
if ... else
and the ability to do an early return
almost anything that you'd need a label for can be written in a cleaner structure.

- 23,369
- 6
- 54
- 74
-
Thanks @jeremy for you comment. Is there any study available online where it is explained why it break labels should be used rarely? – ripu1581 Mar 25 '13 at 04:18
-
None that I know of -- but if you study the regular control structures you'll understand that you really don't need the labels. If you end up with some block you can't simplify, ask for specific help. – Jeremy J Starcher Mar 25 '13 at 04:23
Labels are generally used in JavaScript to break from an outer loop. For example:
while (true) {
while (true) {
if (someCondition) break;
}
}
In this program you'll only break
from the inner loop. The outer loop will loop infinitely. Sometimes however you may wish to break from the outer loop instead of the inner loop. You use labels for this purpose:
loop: while (true) {
while (true) {
if (someCondition) break loop;
}
}
That being said if you have lots of nested loops or switch cases then using labels does in fact help with code readability even though it may not actually affect the program itself.
Take the code in this question for example: https://codereview.stackexchange.com/q/14532/15640
Here we have a switch case in a for loop and although it using we never need to break out of the for loop labelling the loop and the switch case might help some people. For others it's just a distraction.
In the end it boils down to your style of programming. Beginners usually like to label everything. More experience programmers find too many comments and labels annoying.
Steve Yegge actually wrote an entire blog post on this. You may find it very interesting: http://steve-yegge.blogspot.in/2008/02/portrait-of-n00b.html
Try coding without comments: http://www.codinghorror.com/blog/2008/07/coding-without-comments.html

- 1
- 1

- 72,912
- 30
- 168
- 299
I'm hard-pressed to consider a time where requiring nested loops, and breaking the outer by name from within the inner is necessary, versus, perhaps a simple check of a value at the bottom of the outer loop, to decide whether to continue or not...
Typically, that structure is going to be easier to follow than break third_from_outer_loop;
or continue loop_that_is_two_loops_higher
.
If you're at a point where you're ready to break the outermost loop from the innermost, then why not just return early?

- 26,167
- 5
- 41
- 49