JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow break

break statement

Break from the current statement flow or end the current loop.

Description

Ends the current for, while, do....while loop or switch or label statement and passes control to the next statement.

Syntax


Signature Description
break [label];Ends the current for, while, do....while loop or switch or label statement and passes control to the next statement.

Parameters


Parameter Description
labelAn optional identifier associated with a label statement.

Examples

The code below shows an example of the break statement.


// execute the loop until break.
for (var i=0; i<4; i++) {
  if (i == 2) {
    break; 
  }
  alert('The i variable is = ' + i);
}
alert('left for loop');

Press the button below to action the above code:

The code below shows an example of the break and label statements.


// execute the loop until loop condition is false.
for (var i=1; i<3; i++) {
  atLabel:
  for (var k=1; k<3; k++) {
    if (k == 2) {
      break atLabel; 
    }
    alert('The i variable is = ' + i + ' and k variable is = ' + k);
  }
}
alert('left for loop');


Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 4 - While and Do....While Loops
JavaScript Intermediate Tutorials - Lesson 5 - For Loops



  

go to home page Homepage go to top of page Top