JavaScript(JS) break Statement

JavaScript break Statement

In JavaScript, the break statement is used to terminate a loop (for loop, while loop, or do...while loop) or a switch statement before its normal end. When the break statement is encountered inside a loop or switch, the loop or switch is terminated and the control is transferred to the next statement after it.

Here's an example that demonstrates how to use the break statement in a for loop:

refer to‮ditfigi:‬ea.com
for (let i = 1; i <= 10; i++) {
  if (i === 6) {
    break;
  }
  console.log(i);
}

In this example, the for loop iterates from 1 to 10. When i becomes equal to 6, the break statement is executed, and the loop is terminated. As a result, only the numbers from 1 to 5 are printed to the console.

Similarly, you can use the break statement in a while or do...while loop to terminate the loop prematurely.

Here's an example that demonstrates how to use the break statement in a switch statement:

let day = 'Monday';

switch (day) {
  case 'Monday':
    console.log('Today is Monday.');
    break;
  case 'Tuesday':
    console.log('Today is Tuesday.');
    break;
  case 'Wednesday':
    console.log('Today is Wednesday.');
    break;
  default:
    console.log('Today is not Monday, Tuesday, or Wednesday.');
    break;
}

In this example, the switch statement checks the value of the day variable and prints a message to the console based on its value. When the break statement is encountered inside a case, it terminates the switch statement and transfers the control to the statement following it.

In summary, the break statement is used to terminate a loop or a switch statement prematurely.

break with Nested Loop

In JavaScript, you can use the break statement to terminate a nested loop prematurely. When the break statement is executed inside a nested loop, it terminates the innermost loop and transfers the control to the statement immediately following the inner loop.

Here's an example that demonstrates how to use the break statement with a nested loop:

for (let i = 1; i <= 3; i++) {
  console.log(`Outer loop iteration ${i}`);

  for (let j = 1; j <= 3; j++) {
    console.log(`  Inner loop iteration ${j}`);

    if (i === 2 && j === 2) {
      console.log('    Breaking out of inner loop');
      break;
    }
  }
}

In this example, we have a nested loop where the outer loop iterates from 1 to 3, and the inner loop iterates from 1 to 3 for each iteration of the outer loop. Inside the inner loop, we check if the value of i is 2 and the value of j is 2. If it is true, we print a message to the console and break out of the inner loop.

When you run this code, you will see the following output:

Outer loop iteration 1
  Inner loop iteration 1
  Inner loop iteration 2
    Breaking out of inner loop
Outer loop iteration 2
  Inner loop iteration 1
Outer loop iteration 3
  Inner loop iteration 1
  Inner loop iteration 2

As you can see, the inner loop is terminated prematurely when i is equal to 2 and j is equal to 2. After the inner loop is terminated, the outer loop continues its execution, and the control is transferred to the statement immediately following the inner loop.

In summary, the break statement can be used to terminate a nested loop prematurely by breaking out of the inner loop.

JavaScript Labeled break

In JavaScript, you can use a labeled break statement to terminate a loop that is not the innermost loop. A labeled break statement is identified by a label that is attached to the statement. Here's an example:

outerloop:
for (let i = 1; i <= 3; i++) {
  console.log(`Outer loop iteration ${i}`);

  for (let j = 1; j <= 3; j++) {
    console.log(`  Inner loop iteration ${j}`);

    if (i === 2 && j === 2) {
      console.log('    Breaking out of outer loop');
      break outerloop;
    }
  }
}

In this example, we have a labeled break statement with the label outerloop. This label is attached to the outer loop using the colon (:) notation. Inside the inner loop, we check if the value of i is 2 and the value of j is 2. If it is true, we print a message to the console and break out of the outer loop using the labeled break statement.

When you run this code, you will see the following output:

Outer loop iteration 1
  Inner loop iteration 1
  Inner loop iteration 2
    Breaking out of outer loop

As you can see, the labeled break statement terminates the outer loop prematurely when i is equal to 2 and j is equal to 2. The control is transferred to the statement immediately following the outer loop.

In summary, a labeled break statement is a powerful feature that allows you to terminate a loop that is not the innermost loop.