ES6 - The break statement


Advertisements

The break statement is used to take the control out of a construct. Using break in a loop causes the program to exit the loop. Following is an example of the break statement.

Example

var i = 1
while(i< = 10) {
   if (i % 5 == 0) {
      console.log("The first multiple of 5 between 1 and 10 is : "+i)
      break //exit the loop if the first multiple is found
   }
   i++
}

The above code prints the first multiple of 5 for the range of numbers within 1 to 10.

If a number is found to be divisible by 5, the if construct forces the control to exit the loop using the break statement. The following output is displayed on successful execution of the above code.

The first multiple of 5 between 1 and 10 is: 5
Advertisements