Back to JavaScript
2025-12-307 min read

Using a labeled continue with for loops (JavaScript)

Learn Using a labeled continue with for loops (JavaScript) step by step with clear examples and exercises.

Title: Using a Labeled Continue with For Loops (JavaScript)

Why This Matters

In JavaScript, labeled statements allow you to jump out of or continue loops from any point within the loop's body. The continue statement is used to skip iterations in a loop, and when combined with a labeled statement, it can help refine your code and make it more readable. This lesson will teach you how to use a labeled continue statement with for loops in JavaScript.

Why Use Labeled Continue?

Using labeled continue can improve the organization and readability of your code, especially when working with complex nested loops or multiple loops within a function. By labeling a loop, you can jump out of it or skip iterations more precisely, making it easier to understand the flow of your program.

Prerequisites

Before diving into the core concept, ensure you have a good understanding of the following topics:

  1. Basic JavaScript syntax
  2. For loops in JavaScript
  3. Break and continue statements in JavaScript
  4. Variable declarations and assignments
  5. Understanding of block scoping (let, const, and var)
  6. Event loop and callback functions
  7. Promises and async/await concepts
  8. Understanding of arrow functions
  9. Comprehension of nested loops
  10. Knowledge of JavaScript error handling

Core Concept

A labeled continue statement skips the current iteration of a loop and moves on to the next one. When using a labeled continue, you can specify which loop to continue by labeling it with an identifier before the loop declaration. In JavaScript, you can use any valid identifier as a label, but it's common practice to choose a descriptive name that helps you understand the purpose of the loop.

Here's an example demonstrating how to use a labeled continue statement in a for loop:

let numbers = [1, 2, 3, 4, 5];
loop1:
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === 3) { // Check if the current number is 3
console.log("Skipping iteration with number 3...");
continue loop1; // Continue the loop1, not the outer loop
}
console.log(`Current number: ${numbers[i]}`);
}

In this example, we have an array of numbers and a labeled for loop (loop1). Inside the loop, we check if the current number is 3. If it is, we print a message indicating that we're skipping the iteration and continue with the next one in the same loop (loop1) using the continue statement followed by the label of the loop.

Labeling Loops

When labeling loops, it's essential to choose a descriptive name that accurately represents the purpose of the loop. This makes your code easier to understand and maintain over time. Here are some examples of good labels:

outerLoop:
for (let i = 0; i < array1.length; i++) {
innerLoop:
for (let j = 0; j < array2.length; j++) {
// Loop code here...
}
}

In the above example, outerLoop and innerLoop are descriptive labels that help you understand the structure of the nested loops.

Worked Example

Let's create a simple example where we use a labeled continue to filter out even numbers from an array:

let numbers = [2, 4, 6, 8, 10];
filterOddNumbers:
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) { // If the number is even
console.log("Skipping even number:", numbers[i]);
continue filterOddNumbers; // Continue with the next iteration in the loop
}
}
console.log("Filtered odd numbers:", numbers);

Output:

Skipping even number: 2
Skipping even number: 4
Skipping even number: 6
Skipping even number: 8
Filtered odd numbers: [10]

Common Mistakes

  1. Forgetting to label the loop before using a labeled continue.
  2. Using the wrong label when continuing a loop. Make sure the label matches the one you've used for the loop.
  3. Not properly understanding how the labeled continue works and accidentally skipping iterations in an unintended loop.
  4. Incorrectly using the labeled continue with other control flow statements like break.
  5. Labeling multiple loops with the same identifier in the same scope, which can lead to errors when using a labeled continue.
  6. Misusing async/await functions or Promises with labeled continue, as they don't follow the same execution context as regular JavaScript functions.
  7. Using a labeled continue when a simple if statement would be more appropriate for filtering out values from an array or collection.
  8. Incorrectly using a labeled continue inside an arrow function, which may lead to unexpected results due to the function's lexical scoping.
  9. Forgetting to handle errors that might occur when using labeled continue in conjunction with error-prone operations like asynchronous calls or user input validation.

Common Mistakes - Subheadings

  1. Labeling Errors
  • Forgetting to label a loop before using a labeled continue.
  • Using an incorrect or missing label when continuing a loop.
  1. Misunderstanding Labeled Continue
  • Skipping iterations in unintended loops due to a lack of understanding about how the labeled continue works.
  1. Inappropriate Use of Labeled Continue
  • Using a labeled continue when a simple if statement would be more appropriate for filtering out values from an array or collection.
  • Incorrectly using labeled continue with other control flow statements like break.
  1. Label Conflicts
  • Labeling multiple loops with the same identifier in the same scope, which can lead to errors when using a labeled continue.
  1. Async/Await and Promises
  • Misusing async/await functions or Promises with labeled continue, as they don't follow the same execution context as regular JavaScript functions.
  1. Arrow Function Issues
  • Using a labeled continue inside an arrow function, which may lead to unexpected results due to the function's lexical scoping.
  1. Error Handling
  • Forgetting to handle errors that might occur when using labeled continue in conjunction with error-prone operations like asynchronous calls or user input validation.

Practice Questions

  1. Write a JavaScript function that takes an array of numbers and returns a new array containing only the odd numbers using a labeled continue.
  2. Given the following code snippet, identify any potential issues with the use of labeled continue:
let names = ["Alice", "Bob", "Charlie", "Dave"];
outerLoop:
for (let i = 0; i < names.length; i++) {
for (let j = 0; j < names[i].length; j++) {
if (names[i][j] === 'e') { // If the character is 'e'
continue outerLoop; // Continue with the next iteration in the outer loop
}
}
console.log(names[i]);
}
  1. Rewrite the following code using a labeled continue to improve readability and organization:
let array1 = [1, 2, 3, 4];
let array2 = [5, 6, 7, 8];
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
if (array1[i] + array2[j] > 9) {
console.log(`${array1[i]} + ${array2[j]} is greater than 9`);
}
}
}
  1. Write a JavaScript function that takes an array of numbers and returns the sum of all even numbers using a labeled continue.
  2. Given the following code snippet, identify any potential issues with the use of labeled continue:
let names = ["Alice", "Bob", "Charlie", "Dave"];
outerLoop:
for (let i = 0; i < names.length; i++) {
for (let j = 0; j < names[i].length; j++) {
if (names[i][j] === 'e') { // If the character is 'e'
continue outerLoop; // Continue with the next iteration in the outer loop
}
console.log(`Name: ${names[i]}`);
}
}

FAQ

  1. Can I use a labeled continue inside an arrow function?
  • No, you cannot directly use a labeled continue inside an arrow function because arrow functions do not have their own scope and do not support labeled statements. Instead, consider using a regular function with a label for better organization and readability.
  1. What happens if I label multiple loops with the same identifier in the same scope?
  • Labeling multiple loops with the same identifier in the same scope will cause errors when using a labeled continue to continue one of the loops, as JavaScript will not know which loop you meant to continue.
  1. Can I use a labeled continue inside an async function or a Promise?
  • No, you cannot directly use a labeled continue inside an async function or a Promise because they do not follow the same execution context as regular JavaScript functions. Instead, consider using other control flow structures like try-catch blocks or recursive functions to achieve similar results.
  1. How can I handle errors when using labeled continue?
  • You should use error handling techniques such as try-catch blocks to catch any errors that might occur during the execution of your code, especially when working with asynchronous operations like Promises or user input validation.
  1. Is it a good practice to use labeled continue in every loop?
  • No, using labeled continue should be used sparingly and only when necessary for improving the readability and organization of your code. Simple loops with straightforward logic may not require the use of labeled continue.
Using a labeled continue with for loops (JavaScript) | JavaScript | XQA Learn