How to use Break Statement in JavaScript Program

Break Statement

On this page

Break Statement

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

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