Imagine we’re looking to adopt a dog. We plan to go to the shelter every day for a year and then give up. But what if we meet our dream dog on day 65? We don’t want to keep going to the shelter for the next 300 days just because our original plan was to go for a whole year. In our code, when we want to stop a loop from continuing to execute even though the original stopping condition we wrote for our loop hasn’t been met, we can use the keyword break
.
The break
keyword allows programs to “break” out of the loop from within the loop’s block.
Let’s check out the syntax of a break
keyword:
for (let i = 0; i < 99; i++) { if (i > 2 ) { break; } console.log('Banana.'); } console.log('Orange you glad I broke out the loop!');
This is the output for the above code:
Banana. Banana. Banana. Orange you glad I broke out the loop!
break
statements can be especially helpful when we’re looping through large data structures! With breaks, we can add test conditions besides the stopping condition, and exit the loop when they’re met.
Instructions
Log each element from rapperArray
in a for
loop with the iterator variable i
.
After the for
loop, log the string "And if you don't know, now you know."
to the console. Note: since there’s a single quote character, '
, in our string, we can use double quotes around the string to make sure character prints.
Add a break
inside your loop’s block that breaks out of the loop if the element at the current index in the rapperArray
is 'Notorious B.I.G.'
.
Log the element before breaking out of the loop.