Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Loop Control in JavaScript

Explore Loop Control in JavaScript with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers loop control in JavaScript with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

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

 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! 

How to 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 BreakStatement 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

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.