1

Can you explain to me how to use labels on statements that interact with break: switch, while, do, and for.

with example please.

7
  • 1
    What specific problem are you having? Can you show some code you don't understand? Commented May 28, 2012 at 14:13
  • What is the question? What are you having trouble with? Commented May 28, 2012 at 14:13
  • 2
    I've never, ever, ever needed to use a label in JS programming. Commented May 28, 2012 at 14:13
  • 3
    Ohhhhh, I never knew those existed in JS. It's like a goto statement. Don't use them. Commented May 28, 2012 at 14:14
  • 2
    down votes are a bit harsh - it may not be a well written question, and labels might well be next only to eval in evilness, but there's no harm in him asking. Commented May 28, 2012 at 14:16

5 Answers 5

10

Commonly, I see it in breaking out to outer loops:

var i, j;

dance: for (i = 0; i < 20; i++) {
    for (j = 0; j < 20; j++) {
        console.log(i+'-'+j);
        if (j === 10) { //normally, break only breaks the immediate loop
            break dance; //this one breaks the loop labelled as dance
        }
    }
}​

//continue here after i = 0,j = 10
Sign up to request clarification or add additional context in comments.

1 Comment

a "case label" and a break/continue label are very different things.
1

Here's a good article on the GOTO label in JS. I don't ever use GOTO label logic, so I actually learned something new today as well.

JS code from the article:

var pastures = getPastures();
 var i, pastureLen = pastures.length;

pastureLoop:
 for (i = 0; i < pastureLen; i++)
 {
    var pasture = pastures[i];
    var cows = pasture.getCows();

   var j, numCows = cows.length;
    for (j = 0; j < numCows; j++)
    {
       var cow = cows[j];
       if (cow.isEating())
          { continue pastureLoop; }
    }

   // No cows were eating, so fire the callback for pasture[i]
    pasture.executeCallback();    // or whatever
 }

1 Comment

The article link is broken
1

Quouting the Mozilla Developer Network Language Reference:

Provides a statement with an identifier that you can refer to using a break or continue statement.

For example, you can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

Note that they also say:

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

4 Comments

This does not answer the question. Besides, why would break <label>; be more unclear than break; ? The problem is with break, not labels.
@SamuelRossille I'm just quoting the guys that developed the language in the first place - and note that the same page does describe how to use it, and hence does answer the question.
I don't use them myself, but it always seems to me there is a circularity in this argument, since in many cases the only reason they would make code "harder to read" would be because people don't use them and miss the point when looking at the code. In which case, this could be said of any language feature: using it will make the code harder to read for people who don't use or understand the feature. This has just become a form of liturgy over time ("repeat after me...").
@goldilocks a competent programmer would indeed have no problem with this concept. However a competent programmer wouldn't need to ask in the first place...
0

Do not use labels.

Example:

// no label
while (condition) {
    // do something
}

Comments

0

label: (labelled statement) basically used with break or continue statements which helps to either break or continue the labelled statement unlike normal break statement which only breaks the immediate loop.

Let me explain with a demo for both break and continue.

Break :

let i, j;

loop1:
for (i = 0; i < 3; i++) {
  loop2:
  for (j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
        break loop1; // terminate the whole loop labelled as loop1 unline normal break which will only terminate the current loop)
    }
    console.log('i = ' + i + ', j = ' + j);
  }
}

Continue :

let str = '';

loop1:
for (let i = 0; i < 5; i++) {
  if (i === 1) {
    continue loop1; // This statement will again run the loop labelled as loop1
  }
  str = str + i;
}

console.log(str); // '0234'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.