![]() |
The labels and the break statement are used within loops and control flow structures to enhance flexibility and manageability. Each serves distinct purposes and can be applied in the various scenarios based on the needs of the code. These are the following topics that we are going to discuss: What is a Label?A label in JavaScript is an identifier followed by a colon (:) that is applied directly before a statement. It allows to name the loops or blocks of code so that we can refer to them explicitly from other parts of your program. Syntax:outerLoop: for (...) Characteristics:
Applications:
Example: outerLoop and innerLoop are labels marking the for the loops. The break outerLoop statement terminates both the loops when the inner loop condition j === 1 is met.
Output i: 0, j: 0 i: 0, j: 1 What is break?The break statement in JavaScript is used to the terminate the current loop or switch statement and transfers control to the statement immediately following the terminated statement. Syntax:or (...) Characteristics:
Applications:
Example: The loop iterates from the 0 to 4. When i equals 2 the break statement terminates the loop immediately.
Output 0 1 2 Difference Between label and break in JavaScript
ConclusionThe Labels and the break statement are essential features in JavaScript that facilitate structured and efficient control flow. The Labels provide named targets within the code blocks allowing for the precise control over nested loops or custom error handling. On the other hand, break terminates loops and switch statements based on the conditions enabling flexible execution paths within JavaScript programs. |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |