ScanSkill

break

The break statement terminates the running loop, switch, and labeled statements.

Syntax

break

Example

let i = 10;

while (i > 0) {
  console.log(i);
  i--;
  if(i === 8) {
    console.log("break initialized");
    break;
  }
}
/* Prints: 
10
9
8
break initialized
*/

const day = "1";

switch (day) {
  case "1":
    console.log("sunday");
    break;
  case "2":
    console.log("Monday");
    break;
  case "3":
    console.log("tuesday");
    break;
  case "4":
    console.log("wednesday");
    break;
  case "5":
    console.log("thrursday");
    break;
  case "6":
    console.log("friday");
    break;
  case "7":
    console.log("saturday");
    break;
  default:
    console.log("Only from 1 to 7");
    break;
}
// Prints: sunday

The break statement breaks from the running block of the loop, and switch.