JavaScript break Statement

The break statement terminates the loop immediately when it's encountered.

Here's a quick example of the break statement. You can read the rest of the tutorial for more.

Example

// program to print the value of i
for (let i = 1; i <= 5; i++) {
    // break condition
    if (i == 3) {
        break;
    }
    console.log(i);
}

// Output:
// 1
// 2

Here, break terminates the loop when i becomes 3. Hence, the output doesn't include values after 2.


Working of JavaScript break Statement

The image below shows the working of the break statement in for and while loops.

Working of break statement in JavaScript
Working of JavaScript break Statement

Note: The break statement is usually used inside decision-making statements such as if...else.


Example: JavaScript break With while Loop

We can terminate a while loop using the break statement. For example,

// program to find the sum of positive numbers
// the while loop runs infinitely
// loop terminates only when user enters a negative number

let sum = 0;

// infinite loop
while (true) {

    // get number input
    let num = Number(prompt("Enter a number: "));

// terminate the loop if num is negative if (num < 0) { break; }
// otherwise, add num to sum else { sum += num; } } // print the sum console.log(`Sum: ${sum}`);

Output

Enter a number: 3
Enter a number: 5
Enter a number: 0
Enter a number: 8
Enter a number: -3
Sum: 16

The above program continuously asks for user input inside an infinite while loop.

The infinite loop only terminates when the user enters a negative number.


More on JavaScript break Statement

JavaScript break With Nested Loop

When break is used inside two nested loops, it terminates the inner loop. For example,

// nested for loops

// outer loop
for (let i = 1; i <= 3; i++) {

    // inner loop
    for (let j = 1; j <= 3; j++) {
        if (i == 2) {
          break;
        }
        console.log(`i = ${i}, j = ${j}`);
    }
}

Output

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

In the above program, the break statement executes when i == 2. It terminates the inner loop and the control flow moves to the outer loop.

Hence, the value of i = 2 is never displayed in the output.

Using break inside a nested loop
Using break inside a nested loop
Using break with labels.

When using nested loops, we can terminate the outer loop with a labeled break statement.

Working of labeled break statement in JavaScript
Working of labeled break statement in JavaScript

As you can see in the image above, the outerloop label identifies the outer loop.

When break outerloop; is encountered, it exits the outer loop, and control shifts to the statements following it.

Let's look at an example.

outerloop: for (let i = 1; i <= 3; i++) {
  
    innerloop: for (let j = 1; j <= 3; j++) {

if (j === 3) { break outerloop; }
console.log("i = " + i + ", j = " + j); } }

Output

i = 1, j = 1
i = 1, j = 2

In the above example, we have labeled our loops as:

outerloop: for (let i = 1; i <= 3; i++) {...}
innerloop: for (let j = 1; j <= 3; j++) {...}

This helps us identify the loops. Notice the use of the labeled break statement:

if (j === 3) {
    break outerloop;
}

Here, the break statement will terminate the loop labeled as outerloop.

Using break in a switch statement.

We can also use the break statement within a switch statement to terminate a case. For example,

let fruit = "Apple";

switch (fruit) {
    case "Banana":
        console.log("Banana is good!");
        break;
    case "Apple":
        console.log("Apple is tasty!");
        break;
    default:
        console.log("Unknown fruit!");
}

// Output: Apple is tasty!

In this example, the case for Apple is executed, and then break terminates the switch statement.


Also Read:

Video: JavaScript break Statement

Did you find this article helpful?