Ticker

6/recent/ticker-posts

Introduction to the JavaScript `break` Statement

Introduction to the JavaScript `break` Statement

Introduction to the JavaScript `break` Statement

In JavaScript, the `break` statement is used to exit a loop prematurely. It allows you to terminate the execution of a loop and resume program execution from the next statement after the loop. In this article, we will introduce the `break` statement in JavaScript and provide examples to illustrate its usage.

Syntax of the `break` Statement

The basic syntax of the `break` statement in JavaScript is as follows:

    
      break;
    
  

Example:

Consider the following example that uses a `break` statement to exit a loop when a certain condition is met:

    
      for (var i = 1; i <= 5; i++) {
        if (i === 3) {
          break;
        }
        console.log(i);
      }
    
  
JavaScript `break` Statement in Nested Loops

JavaScript `break` Statement in Nested Loops

In JavaScript, the `break` statement can also be used in nested loops to terminate the innermost loop and resume execution from the next statement after the loop. It allows you to break out of multiple levels of nested loops when a certain condition is met. In this article, we will explore the usage of the `break` statement in nested loops with examples.

Example:

Consider the following example that uses a `break` statement to exit nested loops when a specific condition is satisfied:

    
      for (var i = 1; i <= 3; i++) {
        for (var j = 1; j <= 3; j++) {
          if (i === 2 && j === 2) {
            break;
          }
          console.log("i: " + i + ", j: " + j);
        }
      }
    
  
JavaScript `break` Statement with Labeled Loops

JavaScript `break` Statement with Labeled Loops

In JavaScript, the `break` statement can be used with labeled loops to specify which loop should be terminated. This is useful when you have nested loops and want to break out of a specific outer loop. In this article, we will explore the usage of the `break` statement with labeled loops in JavaScript with examples.

Example:

Consider the following example that uses a labeled loop and a `break` statement to exit the outer loop when a certain condition is met:

    
      outerLoop: for (var i = 1; i <= 3; i++) {
        for (var j = 1; j <= 3; j++) {
          if (i === 2 && j === 2) {
            break outerLoop;
          }
          console.log("i: " + i + ", j: " + j);
        }
      }
    
  
JavaScript `break` Statement in Switch Statements

JavaScript `break` Statement in Switch Statements

In JavaScript, the `break` statement can be used in a switch statement to exit the switch block. It is used to prevent the execution of subsequent case statements after a specific condition is satisfied. In this article, we will explore the usage of the `break` statement in switch statements with examples.

Example:

Consider the following example that uses a `break` statement to exit a switch statement when a particular case is matched:

    
      var day = "Wednesday";

      switch (day) {
        case "Monday":
          console.log("It's Monday.");
          break;
        case "Tuesday":
          console.log("It's Tuesday.");
          break;
        case "Wednesday":
          console.log("It's Wednesday.");
          break;
        default:
          console.log("It's another day.");
      }
    
  

Post a Comment

0 Comments