Learn JavaScript: Loops. The break Keyword
This is an exersise from a Codecademy Javascript-tutorial, that I've made today 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++){ console.log('Banana.'); if (i > 3) { break; }; }; console.log('Orange you glad I broke out the loop!'); This is the output for the code: Banana Banana Banana Orange you glad I broke out the loop! ...