0
0
Javascriptprogramming~20 mins

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

Choose your learning style9 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.