57

I just found out about using label s in JavaScript, such as:

for (var i in team) {
    if(i === "something") {
        break doThis: //Goto the label
    } else {
        doThat();
    }
}

doThis: //Label
doIt();

I've not heard about this until now and I can't find much information online about it and I'm beginning to think there is a reason for that.

It seems to me like this is similar to a GOTO statement in other languages and would be considered bad practice. Would I be right in assuming this?

kguest
  • 3,804
  • 3
  • 29
  • 31
Ryan
  • 3,726
  • 3
  • 26
  • 38
  • 9
    your use of labels is incorrect: you need to add the label to your looping construct, ie `doThis: for(...` – Christoph Feb 05 '11 at 12:16
  • 5
    Also **not using** `hasOwnProperty` is definitely bad practice. See: http://bonsaiden.github.com/JavaScript-Garden/#hasownproperty – Ivo Wetzel Feb 05 '11 at 12:16
  • 2
    @IvoWetzel if it's an object that doesn't inherit, it's fine to not use `hasOwnProperty`. Nothing on the `Object` prototype is enumerable. – ZachB May 23 '16 at 21:05

5 Answers5

60

The labels in JavaScript are used mainly with break, or continue in nested loops to be able to break the outer, or continue the outer loop from the code inside inner loop:

    outer:
    for (let i = 0; i < 10; i++)
    { 
       let k = 5;
       for (let j = 0; j < 10; j++) // inner loop
          if (j > 5) 
               break; // inner 
          else
               continue outer;  // it will go to next iteration of outer loop
    }

If you used continue without 'outer' label, it would go to the next iteration of inner loop. That's why there is a need for labels in Javascript.

tdobek
  • 1,156
  • 1
  • 8
  • 6
32

Those are loop breaker identifiers. They are useful if you have nested loops (loops inside loops) and using these identifiers, you can conditionally specify when and which loop to break out from.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • Ahh, so can you not break out into code that is not inside a loop? – Ryan Feb 05 '11 at 12:18
  • 2
    @Ryan: You do not need to break out of code that is not inside a loop. You are wanting to use something like `GO TO` i suppose. You can put up conditions using `if` instead. – Sarfraz Feb 05 '11 at 12:20
  • `They are useful if you have nested loops`: or a `switch` statement inside a loop, which uses the `break` keyword for control as well. – Armen Michaeli Feb 08 '18 at 12:52
15

2020 edit, according to MDN:

Labelled loops or blocks are very uncommon. Usually, function calls can be used instead of loop jumps.

My 2015 answer:

Avoid using labels

Labels are not very commonly used in JavaScript since they make programs harder to read and understand. As much as possible, avoid using labels and, depending on the cases, prefer calling functions or throwing an error.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

Gal Margalit
  • 5,525
  • 6
  • 52
  • 56
  • 26
    That text was [removed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label$compare?locale=en-US&to=805661&from=784289) from MDN in May 2015, with [these comments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label$history) in the revision history: *"Removing recommandation not supported by clear evidence." "Clearing technical review. I think it is true that there is no need for the big red banner that discourages labels. Havent found anything that says differently."* – TachyonVortex Jun 12 '15 at 15:48
  • 1
    2017 update: "Labeled loops or blocks are very uncommon. Oftentimes function calls can be used instead of loop jumps." – Gal Margalit Mar 17 '17 at 19:44
  • 2
    I don't get the "function calls can be used instead of loop jump" part, how would one use a function call to `continue` a loop ? – doubleOrt Dec 01 '17 at 22:58
  • ok @GalMargalit, how? eye: for (i=0; i – pashute Jul 22 '20 at 07:02
7

labelled breaks can break out of any block of code not just loops

<p id="test1"></p>
<p id="test2"></p>
<p id="test3"></p>
<p id="test4"></p>
<p id="test5"></p>

test: {                            
    document.getElementById('test1').innerHTML = "test 1 passed";
    document.getElementById('test2').innerHTML = "test 2 passed";
    document.getElementById('test3').innerHTML = "test 3 passed";
    break test;
    document.getElementById('test4').innerHTML = "test 4 passed";
    document.getElementById('test5').innerHTML = "test 5 passed";
}

result:

test 1 passed

test 2 passed

test 3 passed

Pall Arpad
  • 1,625
  • 16
  • 20
0

There is a workaround to use while loops instead of inner for loops for better readability.

Fakipo
  • 182
  • 2
  • 17