How to use Break Statement in JavaScript Program

Program to use break statement JavaScript Language


let a;
let b = 5;
for (a = 0; a <= 10; a++) {

    if (b == a) {
        break;
    }
    console.log("Number =",a);
}

Output


Number = 0
Number = 1
Number = 2
Number = 3
Number = 4

Steps Description

  1. Declare two variables a and b
  2. Initialize two variables with value of 5 respectively a and b
  3. break statement works with loop
  4. The break statement will break the for loop if the condition in the under if statement of the loop is matched.
  5. console.log inbuild function to display a variable number