JavaScript provides high control to handle loop commands and switch commands. There may be a situation when you need to exit the loop without having to go to its endpoint. There may also be situations when you want to jump over a part of the code block and start the next loop.
To handle the above situations, JavaScript provides break and continue commands. These commands are used to immediately exit any loop or to start the next loop of any corresponding loop.
Break command
The break command, introduced briefly with the switch command, is used to exit soon from a loop, leaving the brace area.
Implementation diagram
Implementation diagram of a break statement is as follows:
For example
The following example illustrates the use of the break statement with a while loop . Notice how the loop exits early when x proceeds to 5 and goes to the document.write command (.) right below the closed parenthesis.
- Event (Event) in JavaScript
- Page navigation (Redirect) in JavaScript
- Dialogs - Alert, Prompt, Confirmation in JavaScript
- Void keywords in JavaScript
- Print pages in JavaScript
- Objects in JavaScript
- Tutorial for creating slideshows in JavaScript with 3 easy steps
- 7 Framework JavaScript for mobile application development
- Introduction to 2D Array - 2-dimensional array in JavaScript
- 12 extremely useful tricks for JavaScript programmers
- Arrays and objects in JavaScript are like stories and newspapers!