Suggest an editImprove this articleRefine the answer for “The label statement in JavaScript”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`label` is a rare tool that gives a name to a block of code or a loop, so that `break labelName` or `continue labelName` can say exactly which loop to break out of or continue.** Without a label, `break` and `continue` only affect the nearest loop, which is why labels are used mostly in nested loops when you need to leave the outer one at once. ```javascript outer: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1 && j === 1) break outer; // leaves both loops } } ``` **Key point:** a label is just a name for a loop or a block; it creates no new scope and it is not an equivalent of `goto`.Shown above the full answer for quick recall.Answer (EN)Image**The `label` statement gives a name to a block of code or a loop, so that execution can then be steered with `break` or `continue` by naming exactly which loop to break out of or continue.** It is a rare, rarely used JavaScript tool that makes sense almost exclusively in nested loops. ## Theory ### TL;DR - A label is a name placed in front of a block or a loop: `outer: for (...) { ... }`. - `break outer` terminates the labelled loop entirely, not just the inner one. - `continue outer` skips the current iteration of the labelled outer loop. - Labels do **not** create a new scope and are not an equivalent of `goto`. - Labelled code usually reads worse, so reach for it only when leaving nested loops is otherwise awkward. ### Quick example ```javascript outer: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1 && j === 1) break outer; console.log(`i = ${i}, j = ${j}`); } } ``` **Result:** ``` i = 0, j = 0 i = 0, j = 1 i = 0, j = 2 i = 1, j = 0 ``` ### Syntax A label can be placed in front of a plain block: ```javascript labelName: { // statements } ``` But most often it goes in front of a loop: ```javascript labelName: for (let i = 0; i < 5; i++) { // loop body } ``` The label name is an ordinary identifier followed by a colon. The label and the loop may sit on the same line or on separate lines; that is a matter of style. ### Why a label is needed: leaving nested loops A label is usually introduced when there are **nested loops** and you have to leave not only the inner one but the outer one as well. Without labels, `break` and `continue` affect only the nearest loop. Without a label: ```javascript for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1 && j === 1) break; console.log(`i = ${i}, j = ${j}`); } } ``` **Result:** ``` i = 0, j = 0 i = 0, j = 1 i = 0, j = 2 i = 1, j = 0 i = 2, j = 0 i = 2, j = 1 i = 2, j = 2 ``` Here `break` stops only the **inner** loop, while the outer one (`i`) happily carries on to `i = 2`. With a label: ```javascript outerLoop: // a label for the outer loop for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i === 1 && j === 1) break outerLoop; console.log(`i = ${i}, j = ${j}`); } } ``` **Result:** ``` i = 0, j = 0 i = 0, j = 1 i = 0, j = 2 i = 1, j = 0 ``` Now `break outerLoop` terminates the **whole outer loop**, not just the inner one. ### continue with a label In the same way you can **skip an iteration of the outer loop** when a condition holds: ```javascript outer: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (j === 2) continue outer; // skips the current i console.log(`i = ${i}, j = ${j}`); } } ``` **Result:** ``` i = 0, j = 0 i = 0, j = 1 i = 1, j = 0 i = 1, j = 1 i = 2, j = 0 i = 2, j = 1 ``` As soon as `j` reaches `2`, control moves on to the next iteration of the outer loop. ### What is important to remember | What it does | Example | | --- | --- | | Gives a name to a loop or a block | `outer: for (...) { ... }` | | Lets you leave the outer loop | `break outer;` | | Lets you skip an iteration of the outer loop | `continue outer;` | | Creates a new scope | No | | Use it often? | No, only when necessary | A few more points: - Labels create no new scopes; they are **just names** that `break` and `continue` refer to. - A label cannot be used for arbitrary jumps the way `goto` works in other languages (in C, for example). - A labelled `continue` only works with loops, whereas a labelled `break` also works with a labelled block. ### Common mistakes - **Treating a label as `goto`.** You cannot jump forward or backward to an arbitrary place: you may only leave the labelled construct or move to its next iteration. - **Putting a labelled `continue` on a plain block.** That is a syntax error; `continue` requires the label to sit on a loop. - **Using a label where a function would do.** Extracting the nested loops into a function and using a plain `return` often reads better than `break outer`. - **Confusing a label with a variable declaration.** `outer:` is not a variable and cannot be referenced as a value. - **Overusing labels.** They are rare precisely because they make code harder to follow; take them only when the alternative is genuinely clumsy.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.