Skip to main content

JavaScript: break Statement

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.
Tags
Submitted by arilio666 on October 11, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.