Labels: Names used to label loops and control flow
Labels in Node.js (and JavaScript) are used to name a statement, typically for controlling the flow of loops or conditional statements. Labels can be particularly useful for breaking out of or continuing a specific loop when you have nested loops.
outerLoop: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) {
break outerLoop; // Exits the outer loop when j equals 1
}
console.log(`i = ${i}, j = ${j}`);
}
}
Here outer for loop
is given a Label.
Labels are used with break or continue to exit or skip specific loops. They are most commonly used in nested loops.Usage in break and continue:
Marks the outer loop.Label outerLoop:
Exits the outerLoop when j equals 1, skipping any further iterations of the outer loop.break outerLoop;:
You Might Also Like
Custom Hook : Simplify Code with Reusable Logic
Setting up a reusable data-fetching function to simplify API calls and state management. # State In...
Conditional Rendering with Short Circuit
if you don’t need the else block while rendering components in React Js, you can do like this ```...