JavaScript: break Statement

Profile picture for user arilio666

In javascript, there is a break statement that is used in almost every loop and switch statement. Normally a break is used to cut the connection of a loop or cut the ongoing loop and continue to the next part of the code.

We must use the break statement when using the looping concept and it is vital because the loop will run endlessly if you run the code without break.

Syntax:

break;

Example

for (let num = 1; num <= 10; num++)
{
    // break condition     
    if (num == 8)
    {
        break;
    }
    console.log(num);
}

Output:

1
2
3
4
5
6
7
  • Here I have used a normal break statement where I have initiated num should be less than 10 and given the condition only that num should be equal to 8.
  • When 8 is reached it should break and come out of the loop.
  • Thus break is an efficient statement to get rid of the loop and continue to the rest of the code.