in , ,

JavaScript break Statement

JavaScript break Statement
JavaScript break Statement

In this tutorial, you will learn about the break statement with the help of examples.

Definition and Usage

The break statement leaves a switch statement or a loop (for, for … in, while, do … while).

At the point when the break statement is used with a switch statement, it breaks out of the switch block. This will stop the execution of more execution of code and additionally case testing inside the block.

At the point when the break statement is used in a loop, it breaks the loop and keeps executing the code after the loop (if any).

The break statement can likewise be used with a discretionary label reference, to “leap out” of any JavaScript code block (see “More Examples” beneath).

Note: Without a label reference, the break statement can only be used inside a loop or a switch.



The break statement is used to end the loop quickly when it is encountered.


The syntax of the break statement is:

break [label];

Note: label is optional and rarely used.


Working of JavaScript break Statement

 


Example 1: break with for Loop

// 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

In the above program, the for loop is used to print the value of i in each iteration. The break statement is used as:

if(i == 3) {
    break;
}

This implies, when I is equivalent to 3, the break statement ends the loop. Subsequently, the output does exclude values greater than or equivalent to 3.

Note: The break statement is quite often used with decision-making statements. To find out additional, visit JavaScript if…else Statement.

To learn more about for loop, visit JavaScript for loop


Example 2: break with while Loop

// program to find the sum of positive numbers
// if the user enters a negative numbers, break ends the loop
// the negative number entered is not added to sum

let sum = 0, number;

while(true) {

    // take input again if the number is positive
    number = parseInt(prompt('Enter a number: '));

    // break condition
    if(number < 0) {
        break;
    }

    // add all positive numbers
    sum += number;

}

// display the sum
console.log(`The sum is ${sum}.`);

Output

Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: -5
The sum is 6. 

In the above program, the user enters a number. The while loop is used to print the total sum of numbers entered by the user.

Here the break statement is used as:

if(number < 0) {
    break;
}

At the point when the user enters a negative number, here – 5, the break statement ends the loop and the control flow of the program goes outside the loop.
In this manner, the while loop proceeds until the user enters a negative number.

To learn more about while loop, visit JavaScript while loop.


break with Nested Loop

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

// nested for loops

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

    // second 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, when I == 2, break statement executes. It ends the internal loop and control stream of the program moves to the external loop.
Henceforth, the value of I = 2 is never shown in the output.


JavaScript Labeled break

When using nested loops, you can likewise end the external loop with a label statement.
Anyway, the labeled break is rarely used in JavaScript because this makes the code harder to read and comprehend.

If you want to learn more about the labeled break statements, visit labeled break.


salman khan

Written by worldofitech

Leave a Reply

JavaScript while and do…while Loop

JavaScript while and do…while Loop

WhatsApp to stop working on these smartphones from January 1

WhatsApp to stop working on these smartphones from January 1