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:

Loop control in JavaScript Picture 1

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.

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result

 

 Entering the loop 
2
3
4
5
Exiting the loop!
Đặt biến vào giá trị khác nhau và thử thử .

We have seen the usage of the break statement within a switch statement .

Continue statement

The continue statement tells the interpreter to immediately start the next iteration of the loop and jump over the remaining code block. When a continue statement is encountered, the program flow moves immediately to the test expression and if the condition is true, then it starts the next iteration, otherwise the control exits the loop.

For example

This example illustrates the use of the continue statement with a while loop. Notice how the continue statement is used to jump over the print when the index is kept in the x value to 5.

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result

 

 Entering the loop 
2
3
4
6
7
8
9
ten
Exiting the loop!

Use Label to control Flow (Flow)

Starting from JavaScript 1.2, a Label can be used with break and continue to control the flow more accurately. A label is simply an identifier followed by a colon (:) that applies to a command or block of code. We will see two different examples to understand how to use labels with break and continue.

Note - Interrupt lines are not allowed between the continue or break statement and its label name. Also, there should not be any other commands between a label name and a link loop.

Try these two examples to get a deeper understanding of Label.

Example 1

This example shows how to perform Label with a break statement:

  type = "text/javascript" >  

Result

 

 Entering the loop! 
Outerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 1
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 2
Outerloop: 3
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Outerloop: 4
Exiting the loop!

Example 2

This example shows how to perform Label with a continue statement:

  type = "text/javascript" >  

Result

 Entering the loop! 
Outerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 2
Outerloop: 1
Innerloop: 0
Innerloop: 1
Innerloop: 2
Outerloop: 2
Innerloop: 0
Innerloop: 1
Innerloop: 2
Exiting the loop!

 

According to Tutorialspoint

Previous article: For . loop in JavaScript

Next article: Function in JavaScript

4 ★ | 1 Vote

May be interested

  • Syntax of JavaScriptSyntax of JavaScript
    javascript can be implemented using javascript commands that are placed in html tags ... in a web page.
  • CONTINUE command in SQL ServerCONTINUE command in SQL Server
    the continue command is used to give command execution authority to the last expression of the loop. that means upside-down to the top of the loop, all the commands that follow in the loop containing continue will be ignored without execution.
  • What is JavaScript? Can the Internet exist without JavaScript?What is JavaScript?  Can the Internet exist without JavaScript?
    not everyone knows what javascript is and how it works. the long and fascinating development history of javascript as well as what we can do with javascript is still unknown.
  • How to Fix Boot Loop Problems in WindowsHow to Fix Boot Loop Problems in Windows
    after starting your windows device, you may sometimes experience an infinite boot loop issue (also known as an 'infinite boot loop' error).
  • While loop in PythonWhile loop in Python
    what does the while loop in python do? what is the syntax and how to use while loop? those are the content we will approach in this python lesson.
  • What is Currying in Javascript? How to use Currying in JavaScriptWhat is Currying in Javascript? How to use Currying in JavaScript
    the currying feature in javascript can help you keep your code tidy and give you a new way of seeing how functions work. currying is ideal when you want to break complex logic into smaller, manageable, and self-contained pieces of code.
  • What is Loop mail?What is Loop mail?
    what is loop mail? to help you with this question, the following article will help you find out what loop mail is.
  • Things to know about 'this' in JavaScriptThings to know about 'this' in JavaScript
    are you having trouble understanding the keyword 'this' in javascript ? then please read what you need to know about 'this' in javascript below.
  • Udemy's top 5 JavaScript coursesUdemy's top 5 JavaScript courses
    a programming language that runs on any computer in the world. a language does not need any special software to run. a language ranked among the top in the world.
  • Why Microsoft Loop Can't Compare to NotionWhy Microsoft Loop Can't Compare to Notion
    many people have been using notion for brainstorming for years, so it's interesting to see if loop can replace it as they move deeper into the microsoft ecosystem.