Bird
Raised Fist0
Javascriptprogramming~20 mins

Labeled break and continue in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Labeled Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested loops with labeled break
What is the output of this JavaScript code using a labeled break?
Javascript
outerLoop: for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (i * j > 3) {
      break outerLoop;
    }
    console.log(`${i},${j}`);
  }
}
A"1,1\n1,2\n2,1\n2,2\n3,1"
B"1,1\n1,2\n2,1"
C"1,1\n1,2\n2,1\n2,2"
D"1,1\n1,2\n1,3\n2,1\n2,2"
Attempts:
2 left
💡 Hint
Remember that labeled break exits the entire outer loop immediately.
❓ Predict Output
intermediate
2:00remaining
Output of nested loops with labeled continue
What is the output of this JavaScript code using a labeled continue?
Javascript
outer: for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (j === 2) {
      continue outer;
    }
    console.log(`${i},${j}`);
  }
}
A"1,1\n2,1\n3,1"
B"1,1\n1,3\n2,1\n2,3\n3,1\n3,3"
C"1,1\n1,3\n2,1\n3,1"
D"1,1\n2,1\n3,1\n3,3"
Attempts:
2 left
💡 Hint
Labeled continue skips to the next iteration of the outer loop.
🔧 Debug
advanced
2:00remaining
Identify the error with labeled continue usage
What error does this JavaScript code produce?
Javascript
outer: for (let i = 0; i < 2; i++) {
  for (let j = 0; j < 2; j++) {
    if (j === 1) {
      continue inner;
    }
    console.log(i, j);
  }
}
ASyntaxError: Unexpected identifier
BReferenceError: inner is not defined
CNo error, outputs: 0 0 1 0
DTypeError: continue label must be an identifier
Attempts:
2 left
💡 Hint
Check if the label used in continue exists.
❓ Predict Output
advanced
2:00remaining
Output with nested labeled break and continue
What is the output of this JavaScript code?
Javascript
outer: for (let i = 1; i <= 3; i++) {
  inner: for (let j = 1; j <= 3; j++) {
    if (i === j) {
      continue outer;
    }
    if (i + j === 4) {
      break inner;
    }
    console.log(`${i},${j}`);
  }
}
A"1,2\n2,1\n3,1\n3,2"
B"1,2\n1,3\n2,1\n3,1\n3,2"
C"1,2\n2,1\n3,1"
D"1,2\n2,1\n3,2"
Attempts:
2 left
💡 Hint
Remember continue outer skips to next i, break inner exits inner loop.
🧠 Conceptual
expert
2:00remaining
Effect of labeled break on loop execution
Consider this code snippet:
label1: for (let a = 0; a < 2; a++) {
  label2: for (let b = 0; b < 2; b++) {
    if (a === b) {
      break label1;
    }
    console.log(a, b);
  }
}

What is the value of variable a after the loops finish?
A1
Bundefined
C2
D0
Attempts:
2 left
💡 Hint
Labeled break exits the outer loop immediately when condition is met.

Practice

(1/5)
1.

What does the break label; statement do in JavaScript?

easy
A. It exits the loop identified by the label immediately.
B. It skips the current iteration of the inner loop only.
C. It restarts the entire program from the beginning.
D. It pauses the loop until a condition is met.

Solution

  1. Step 1: Understand labeled break

    The break label; statement exits the loop that has the specified label, not just the innermost loop.
  2. Step 2: Compare with normal break

    Normal break exits only the current loop, but labeled break exits the labeled outer loop immediately.
  3. Final Answer:

    It exits the loop identified by the label immediately. -> Option A
  4. Quick Check:

    break label exits labeled loop [OK]
Hint: Labeled break exits the named loop, not just inner loop [OK]
Common Mistakes:
  • Thinking break label only skips current iteration
  • Confusing break label with continue label
  • Assuming break label pauses the loop
2.

Which of the following is the correct syntax to continue the outer loop labeled outerLoop?

outerLoop: for(let i = 0; i < 3; i++) {
  for(let j = 0; j < 3; j++) {
    // What goes here?
  }
}
easy
A. continue outerLoop;
B. continue (outerLoop);
C. continue: outerLoop;
D. continue to outerLoop;

Solution

  1. Step 1: Recall labeled continue syntax

    The correct syntax to continue a labeled loop is continue labelName; without parentheses or extra words.
  2. Step 2: Match options with syntax

    continue outerLoop; matches the correct syntax exactly. Options B, C, and D are invalid syntax.
  3. Final Answer:

    continue outerLoop; -> Option A
  4. Quick Check:

    continue labelName; is correct syntax [OK]
Hint: Use 'continue label;' exactly, no parentheses or colons [OK]
Common Mistakes:
  • Adding parentheses around label
  • Using colon after continue
  • Writing extra words like 'to'
3.

What is the output of the following code?

outer: for(let i = 1; i <= 3; i++) {
  for(let j = 1; j <= 3; j++) {
    if(j === 2) continue outer;
    console.log(i, j);
  }
}
medium
A. 1 1 1 3 2 1 3 1
B. 1 1 2 1 3 1
C. 1 1 1 3 2 1 2 3 3 1 3 3
D. 1 1 2 2 3 3

Solution

  1. Step 1: Understand continue outer;

    When j === 2, continue outer; skips to the next iteration of the outer loop, skipping the rest of inner loop and console.log for that i.
  2. Step 2: Trace iterations

    For i=1: j=1 logs '1 1', j=2 triggers continue outer (skip to i=2). For i=2: j=1 logs '2 1', j=2 triggers continue outer (skip to i=3). For i=3: j=1 logs '3 1', j=2 triggers continue outer (loop ends after i=3).
  3. Final Answer:

    1 1 2 1 3 1 -> Option B
  4. Quick Check:

    continue outer skips inner loop rest [OK]
Hint: continue label skips to next outer loop iteration [OK]
Common Mistakes:
  • Thinking continue outer skips only inner iteration
  • Expecting all inner loop logs
  • Confusing break and continue effects
4.

Find the error in this code snippet:

outerLoop: for(let i = 0; i < 2; i++) {
  for(let j = 0; j < 2; j++) {
    if(i === j) break;
    if(j === 1) continue outerLoop;
    console.log(i, j);
  }
}
medium
A. The label name is missing a colon.
B. Using continue outerLoop; inside inner loop is invalid.
C. The break; statement breaks only inner loop, not labeled loop.
D. No error; code runs correctly.

Solution

  1. Step 1: Analyze break statement

    The break; without label breaks only the innermost loop, not the labeled outer loop.
  2. Step 2: Check labeled continue usage

    The continue outerLoop; is valid and skips to next iteration of the labeled loop.
  3. Final Answer:

    The break; statement breaks only inner loop, not labeled loop. -> Option C
  4. Quick Check:

    break without label affects inner loop only [OK]
Hint: break without label breaks inner loop only [OK]
Common Mistakes:
  • Assuming break breaks labeled loop without label
  • Thinking continue outerLoop is invalid
  • Confusing label syntax
5.

Given the nested loops below, how can you skip all remaining iterations of the outer loop when j === 3?

outerLoop: for(let i = 1; i <= 5; i++) {
  for(let j = 1; j <= 5; j++) {
    if(j === 3) {
      // What statement here?
    }
    console.log(i, j);
  }
}
hard
A. continue outerLoop;
B. break;
C. continue;
D. break outerLoop;

Solution

  1. Step 1: Understand the goal

    We want to stop all remaining iterations of the outer loop when j === 3, meaning exit the outer loop completely.
  2. Step 2: Choose correct statement

    break outerLoop; exits the labeled outer loop immediately. Plain break; exits only inner loop. continue; and continue outerLoop; skip iterations but do not exit the outer loop.
  3. Final Answer:

    break outerLoop; -> Option D
  4. Quick Check:

    break label exits labeled loop [OK]
Hint: Use break label to exit outer loop immediately [OK]
Common Mistakes:
  • Using break without label to exit outer loop
  • Using continue instead of break
  • Confusing continue label with break label
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard