3

What the title says is my question. I'm new to JavaScript. I am wondering if I can use statements to jump to certain lines of code. I have looked here and here. They don't explicitly say this can be done, but I think there might be a way for it to be done.

The way I understand it now it that I can designate any block of code or statement to have a label. I can attach that label to a break or continue statement, and it will jump to that line of code. But it seems, from the W3 tutorial, I can only jump to the top of the block of code where the label is.

It seems pointless to allow a continue statement to have a label proceed it when it can only be used inside of a loop and anything it can do with a label can also be done with a break and a label. In this example there is a break statement used to go to the top of the statement block; is there any difference in using the continue vs break?

I realize this could be bad practice because of what is hisorically known as "spaghetti code," but I am a curious person.

Community
  • 1
  • 1
OKGimmeMoney
  • 575
  • 2
  • 7
  • 19
  • At any rate, unstructured jump techniques shouldn't be part of your day-to-day toolbox in any language. They CAN be useful in certain situations, especially for error handling in languages that don't support exceptions. But JS does, so I can't think of a typical use case. – chris Aug 15 '14 at 21:18
  • `goto` ftw! We're bringing it back! No, don't do it. – Rudie Aug 15 '14 at 21:44

2 Answers2

4

continue ends the current iteration and jumps straight to the next iteration in the loop, so basically to the top of the code block in the loop, if there is a next iteration to do.

break ends the loop, so it jumps past it. No further iterations are performed.

So that's quite a big difference. What they have in common, is that they can have a label to indicate a specific statement. So if you have a nested for loop, you can continue or break the outer for loop if it has a label. That doesn't mean that you jump to the label, it just means that you apply the break or continue to the loop indicated by the label, instead of the inner-level loop which you are in at that moment. You still have to be inside that labeled statement, though. You cannot jump to another part of the program.

What you are asking for is basically a goto statement. For that, maybe you'd like to read this question: How can I use goto in Javascript?

Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 2
    Spot on. Though I can't remember that I ever needed to break or continue to an outer loop. Usually it's much clearer to restructure your code into smaller functions so you can use return instead. – chris Aug 15 '14 at 21:29
  • Me neither. To be honest, I didn't even know about labels in Javascript before answering this question. ;) That's probably because I've never needed them, otherwise I would have looked for it, since you can do this in other languages as well. – GolezTrol Aug 15 '14 at 21:30
0

A break foo; does not "go to the top of a statement block" - it is not a GOTO.

Rather, the name specified informs JavaScript of which loop to break from, which is sometimes (although perhaps rarely) useful for nested loops.

Consider this code which does terminate:

outer: while (true) {
    inner: while (true) {
        break outer; // goes from here -->
    }
}
// <-- to here

And a [flawed] variation, which loops until forcibly terminated:

outer: while (true) {
    inner: while (true) {
        // break; or,
        break inner; // goes from here -->
    }
    // <-- to here
}

On the other hand, continue merely skips the remaining code within the loop - the post actions are performed and the loop condition is re-evaluated. That is, continue does not unconditionally terminate the loop.

user2864740
  • 60,010
  • 15
  • 145
  • 220