This article is about labeled loops in Javascript. We’ll take a glance at the fundamentals and then walk through a couple of practical examples.
Overview
In JavaScript, a labeled loop is a loop that has a label, which is a user-defined identifier that is used to mark a loop. This is useful when you have nested loops, and you want to break out of a specific loop instead of breaking out of all the nested loops.
Basic syntax:
labelName:
for (initialization; condition; expression) {
// code block to be executed
}
labelName is the label that marks the loop. The for loop is the loop statement, which includes the initialization, condition, and expression. The code block to be executed is enclosed in curly braces.
Words can be vague and confusing. Now, let’s take a look at the following real-world use cases of implementing labeled loops in JavaScript.
Examples
Search in Nested Arrays
Let’s say you have a nested array, and you want to find a specific value. You can use a labeled loop to break out of the inner loop when the value is found.
The code:
let arr = [[1, 2], [3, 4], [5, 6]];
outerLoop:
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] === 4) {
console.log("Value found at index " + i + "," + j);
break outerLoop;
}
}
}
Output:
Value found at index 1,1
Skip to the Next Iteration
Sometimes, you might want to skip to the next iteration of a loop based on a certain condition. You can use a labeled loop to achieve this.
Example:
outerLoop: // Label
for (let i = 0; i < 5; i++) {
innerLoop: // Label
for (let j = 0; j < 5; j++) {
if (j === i) {
continue outerLoop;
}
console.log(i, j);
}
}
Output:
1 0
2 0
2 1
3 0
3 1
3 2
4 0
4 1
4 2
4 3
In the example above, the labeled loop outerLoop is used to skip to the next iteration of the outer loop when i is equal to j (the output stops at 4 3).
Breaking out of Multiple Loops
In cases where you have multiple nested loops, you might want to break out of all of them when a certain condition is met. You can use a labeled loop to achieve this.
Example:
outerLoop:
for (let i = 0; i < 5; i++) {
middleLoop:
for (let j = 0; j < 5; j++) {
innerLoop:
for (let k = 0; k < 5; k++) {
if (k === 3) {
break outerLoop;
}
console.log(i, j, k);
}
}
}
Output:
0 0 0
0 0 1
0 0 2
In the code above, the labeled loop outerLoop is used to break out of all three nested loops when k is equal to 3.
When to use labeled loops
Labeled loops in JavaScript can be useful in some situations, such as:
- When working with nested loops and you need to break out of a specific loop rather than all of them.
- When you need to skip to the next iteration of a loop based on a certain condition.
- When you have multiple nested loops, you need to break out of all of them when a certain condition is met.
When NOT to use labeled loops
Sometimes, the use of labels can add unnecessary complexity to the code:
- When the loop is simple and does not have any nested loops.
- When the loop is small and easy to read without labels.
- When the use of labels makes the code harder to understand or maintain.
If you have any questions or suggestions about this tutorial, please leave a comment. We’re more than happy to hear from you.