Loop control in JavaScript
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.