Tutorials Tonight's Logo
Follow Us

BREAK AND CONTINUE STATEMENT IN JAVASCRIPT


The break statement is used to break or jump out of the javascript loops ( like for loop, while loop and do...while loop ) and switch statements.

The continue statement is used to skips the execution of the program to the next iteration.

Let's see the flow diagram of both break and continue statements.

break statement tutorialstonight

Break statement flow diagram

continue statement tutorialstonight

continue statement flow diagram

In the above images you can see comparison between break and continue statements. The break and continue statements are used inside conditional statements, so when certain condition statisfy you can choose to break the loop or choose to continue to next iteration.


Javascript break statement

Javascript break statement helps use to exit out of the loop when certain condition satisfies. For example: we want to exit a loop when our initialize or some variable achieve a value, then we can check for that condition and if condition statisfies then use break statement to exit out of the loop.

We have already seen the break statement when we were learning switch statement. The break statement was used there to jump out of switch statement.

Here is an example of a break statement which jumps out of loop when count (some variable) becomes equal to 4.

var count = 0;
for (count = 0; count < 10; count++) {
if (count === 4) {
    break; // Jumps out of loop when executed
}
console.log(count);
}
▶ Run the code

Javascript continue statement

The continue statement is used to jump one iteration of the loop, means you can skip one time execution of the loop using it. For example: while executing a loop if you want not to work on a variable for some value 4, then you can simply skip it by using continue statement.

In the example below continue statement executes when count is equal to 4, and skip the code below it without executing.

var count = 0;
for (count = 0; count < 10; count++) {
if (count == 4) {
    continue;
}
console.log(count);
}
▶ Run the code

Note: break and continue statements can't be used in forEach, map, reduce, etc because these are functions and break and continue can only be used in loops.


Javascript label statement

Javascript labelled code represents a block of code where the label name is used to identify the block of code.

The label name should not be a reserved keyword in javascript. like true, false, for, etc.

Syntax:

label_name:
        // Single or Multiple Code
        // Statements

A label name can be used to break or continue the block of code.

break label_name;

continue label_name;
fruits: {
    document.getElementById("fruits").innerHTML += "Mango ";
    document.getElementById("fruits").innerHTML += "Apple ";
    break fruits;
    document.getElementById("fruits").innerHTML += "Banana ";
    document.getElementById("fruits").innerHTML += "Orange ";
  }

  numbers:
  for (let i = 1; i <= 5; i++) {
    if (i === 3) {
      continue numbers;
    }
    console.log(count);
  }
▶ Run the code

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.



Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.