< JavaScript < Reserved words

The break keyword

The break keyword interrupts a loop immediately.

Examples

The code
  var array = [2, 3, 5, 7, 10, 11], result = 1;

  for (var i = 0; i < array.length; i++) {
    result += array[i];
    console.log("result = " + result);
    if (result%2 == 0) {
      break;
    }
  }
returns the following:
result = 3
result = 6

See also

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.