Skip to content

SyntaxError: Undefined label

Quick summary

You wrote break outerLoop but no label named outerLoop encloses the loop — the target does not exist.

Why this happens

  • Typo in the label name used by break/continue

  • Label was defined in a different scope or removed during refactoring

  • Using break to exit a loop that is not the labeled one

Minimal example

✗ Broken code
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j === 1) break outer; // SyntaxError: Undefined label 'outer'
  }
}
✓ Fixed code
outer: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j === 1) break outer; // exits the outer loop
  }
}

The label outer is not defined, so break outer fails. Adding the outer: label to the outer loop gives the break a valid target.

How to diagnose

  • Read the SyntaxError: 'Undefined label'

  • Search for the label definition and compare with the break/continue

  • Remove the label if it is unnecessary, or fix the typo

How to fix

  • Correct the label name to match an enclosing labeled statement

  • Add the missing label to the intended outer loop

  • Refactor to avoid labels entirely when possible

How to prevent

  • Avoid labels — use function returns or flags for complex control flow

  • When using labels, keep them close to the break/continue that references them

Related Resources

Related Glossary

Related Lessons

← Back to language