JavaScript Break And Continue Statements


What are break and continue statements in JavaScript?

The break and continue statements are control flow statements used to alter the execution of loops.

  • break: Exits the loop immediately, skipping any remaining iterations.
  • continue: Skips the current iteration and moves to the next iteration of the loop.

How does the break statement work in a loop?

The break statement terminates the loop when it is encountered. The program execution continues with the next statement following the loop.


for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break; // Exit the loop when i equals 5
  }
  console.log(i); // Outputs: 0, 1, 2, 3, 4
}

Can you provide an example of using the break statement in a switch case?

The break statement is used to prevent fall-through behavior in switch cases, ensuring that only the matching case executes.


let fruit = 'banana';

switch (fruit) {
  case 'apple':
    console.log('Apple is red.');
    break;
  case 'banana':
    console.log('Banana is yellow.'); // This will be executed
    break;
  case 'orange':
    console.log('Orange is orange.');
    break;
  default:
    console.log('Unknown fruit.');
}

How does the continue statement work in a loop?

The continue statement skips the current iteration of the loop and moves to the next iteration. It can be used in any type of loop.


for (let i = 0; i < 10; i++) {
  if (i % 2 === 0) {
    continue; // Skip even numbers
  }
  console.log(i); // Outputs: 1, 3, 5, 7, 9
}

Can you provide an example of using continue with a while loop?


let j = 0;
while (j < 10) {
  j++;
  if (j % 2 === 0) {
    continue; // Skip even numbers
  }
  console.log(j); // Outputs: 1, 3, 5, 7, 9
}

What happens if you use break or continue statements in nested loops?

In nested loops, the break statement will exit only the innermost loop where it is called. To exit the outer loop, you can use a labeled statement. The continue statement will only skip to the next iteration of the innermost loop.


outerLoop: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      break outerLoop; // Breaks the outer loop
    }
    console.log(`i: ${i}, j: ${j}`);
  }
}
// Output: i: 0, j: 0
//         i: 0, j: 1
//         i: 0, j: 2
//         i: 1, j: 0
//         i: 1, j: 1 (breaks here)

What are labeled statements, and how do they work with break and continue?

Labeled statements allow you to name a loop or a block of code. This is useful when you have nested loops and want to break or continue a specific outer loop.


outerLoop: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j === 1) {
      continue outerLoop; // Skips to the next iteration of the outer loop
    }
    console.log(`i: ${i}, j: ${j}`);
  }
}
// Output: i: 0, j: 0
//         i: 0, j: 2
//         i: 1, j: 0
//         i: 1, j: 2
//         i: 2, j: 0
//         i: 2, j: 2

How can you use break and continue with array iterations?

You can use break and continue within loops that iterate over arrays, such as for loops, forEach, or other iterative methods.


const numbers = [1, 2, 3, 4, 5];

for (const number of numbers) {
  if (number === 3) {
    continue; // Skip number 3
  }
  console.log(number); // Outputs: 1, 2, 4, 5
}

What are the best practices for using break and continue statements?

Best practices include:

  • Use break and continue judiciously to improve code readability.
  • Ensure that using these statements does not make the code confusing or difficult to follow.
  • Consider alternatives, such as using conditional statements within the loop, to maintain clarity.
Ads