Break and Continue

Learn about the usage of break and continue keywords.

We'll cover the following...

Introduction

We can use break or continue inside the loops:

  • break exits the closest loop it is in and continues with the next command,
  • continue skips out from the loop body and jumps back to the condition of the loop.

Example: sum every second element of the numbers array:

Press + to interact
let numbers = [19, 65, 9, 17, 4, 1, 2, 6, 1, 9, 9, 2, 1];
function sumArray( values ) {
let sum = 0;
for ( let i in values ) {
if ( i % 2 == 0 ) continue;
sum += values[i];
}
return sum;
}
console.log("The sum is", sumArray( numbers ));

Continue

This ...