A Platform To Learn To Code

JAVASCRIPT BREAK AND CONTINUE



The break statement is used to break or jump out of the loop.

The continue statement is used to skip loop body executes for that 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


The break statement

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

switch statement is also used to jump out of loops. It is enclosed in a conditional statement and it is executed when the condition is true.

Here is an example of a break statement which jumps out of loop when count becomes 4.

Example

Run the code

The continue statement

The continue statement is used to jump one iteration of the loop. It skips the execution of code for that iteration when it is executed.

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

Example

Run the code

The label statement

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

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;

Example

Run the code