Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A labeled statement provides an identifier for a statement in JavaScript, allowing control flow directives (break and continue) to explicitly target and alter the execution path of that specific statement.
labelIdentifier: statement

Mechanics and Behavior

Namespace Isolation Labels reside in a distinct internal namespace. A label identifier will not collide with variables, functions, or classes sharing the same name within the same scope. Scope and Visibility A label is only visible within the lexical scope of the statement it prefixes. It cannot be referenced from outside that statement, nor can it be referenced from within nested function boundaries (such as callbacks or closures) inside the labeled statement. Interaction with break The break statement can reference a label attached to any enclosing statement. This includes iteration statements (for, while, do...while), block statements ({}), switch statements, if statements, and try...catch blocks. When executed, break labelIdentifier; immediately terminates the targeted labeled statement and transfers control to the statement immediately following it.
myLabel: if (true) {
  console.log("Execution starts");
  break myLabel; 
  console.log("Unreachable code");
}
console.log("Execution resumes here");
Interaction with continue The continue statement can only reference a label attached to an iteration statement. When executed, continue labelIdentifier; terminates the current iteration of the targeted loop and evaluates the loop’s condition to determine if the next iteration should begin.
outerLoop: for (let i = 0; i < 3; i++) {
  innerLoop: for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      continue outerLoop; // Skips to the next iteration of 'outerLoop'
    }
    console.log(`i=${i}, j=${j}`);
  }
}

Syntax Rules and Restrictions

  • Valid Identifiers: A label must be a valid JavaScript identifier and cannot be a reserved word (e.g., class, return, if).
  • Strict Mode Constraints: In strict mode ("use strict";), the identifier let cannot be used as a label. Additionally, labeling a function declaration throws a SyntaxError.
  • Duplicate Labels: You cannot use the same label identifier more than once within the same nested statement chain. However, sibling statements can share the same label name.
  • Chaining: Multiple labels can be chained to a single statement, though this is syntactically redundant.
// Valid label chaining
labelOne: labelTwo: for (let i = 0; i < 5; i++) {
  break labelOne;
}
Master JavaScript with Deep Grasping Methodology!Learn More