Javascript break Statement

Introduction

The break statement provide control over the loop statement.

The break statement exits the loop immediately, forcing execution to continue with the next statement after the loop.

Here's an example:

let num = 0;/*from  w  w w .ja v  a2s.  co  m*/

for (let i = 1; i < 10; i++) {
    if (i % 5 == 0) {
        break;
    }
    num++;
}

console.log(num); // 4 

In this code, the for loop increments the variable i from 1 to 10.

In the body of a loop, an if statement checks to see if the value of i is evenly divisible by 5.

If so, the break statement is executed and the loop is exited.

The num variable starts out at 0 and indicates the number of times the loop has been executed.

After the break statement has been hit, the next line of code to be executed is the console.log.

The number of times the loop has been executed is four because when i equals 5, the break statement causes the loop to be exited before num can be incremented.

The break statement can be used in conjunction with labeled statements to return to a particular location in the code.

This is typically used when there are loops inside of loops:

let num = 0;//from w  ww.j a  v a 2  s .com

outermost:
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10; j++) {
            if (i == 5 && j == 5) {
                break outermost;
            }
            num++;
        }
    }

console.log(num); // 55 

In this example, the outermost label indicates the first for statement.

Each loop normally executes 10 times.

The break statement here is given one argument: the label to break to.




PreviousNext

Related