Using Labeled break and continue in JavaScript
📖 Scenario: Imagine you are managing a small warehouse. You have a list of shelves, and each shelf has boxes with different items. You want to find a specific item quickly and also skip certain shelves based on a condition.
🎯 Goal: You will write a program that uses labeled break and continue statements to control loops effectively. This will help you stop searching when you find the item or skip shelves that are empty.
📋 What You'll Learn
Create a nested array called
shelves with 3 shelves, each shelf is an array of items (strings).Create a variable called
targetItem with the value 'apple'.Use a labeled outer loop called
searchShelves to go through each shelf.Inside the outer loop, use an inner loop to go through each item in the shelf.
Use
continue searchShelves to skip the shelf if it is empty.Use
break searchShelves to stop all searching when targetItem is found.Print
'Found apple!' when the item is found.💡 Why This Matters
🌍 Real World
In real warehouses or stores, quickly finding items and skipping empty shelves saves time and effort.
💼 Career
Understanding labeled break and continue helps in writing efficient loops in software development, especially when dealing with nested data or complex conditions.
Progress0 / 4 steps