FAQ: Loops - The break Keyword

This community-built FAQ covers the “The break Keyword” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise The break Keyword

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: JS - Why does this code iterate 3 times? [solved]

5 posts were split to a new topic: Troubleshooting code specifics [solved]

A post was split to a new topic: Solution is wrong? [self-solve]

A post was split to a new topic: Code Problem? [Unreproducible]

9 posts were merged into an existing topic: [Bug Reported] Solution code not advancing lesson

4 posts were split to a new topic: Typo in code? [Solved]

A post was merged into an existing topic: [Bug Reported] Solution code not advancing lesson

2 posts were split to a new topic: Difference between “do” and “for” looping [Answered]

I think you guys should add, continue statement to this break chapter

3 Likes

My code for this exercise was:

for (let i = 0; i <= 3; i++) {
  if (i < 3) {
    break;
  }
  console.log(rapperArray[i]);
};
console.log("And if you don't know, now you know.");

When I hit “run” it worked, but the exercise was marked as failed. I do see the “correct” solution, but, what in my code is wrong? I know it would be better to match to BIG since the place in the array can change, but, for the purpose of the exercise I thought this would be okay.

1 Like

In the exercise we are to log the rapper in each iteration and break after we’ve logged, 'Notorious B.I.G.'.

console.log(rapperArray[i]);
if (rapperArray[i] === 'Notorious B.I.G.') {
    break;
}
3 Likes

Once I heard that using break, continue, and keywords that would forcibly change the regular logic are not part of good programming practices. Can someone sahre their wisdom on the matter? Thanks!

1 Like

If they were not part of good practice then why would they exist? We may be able to write code other ways so that we avoid their use, but are they really something to avoid?

Their purpose is simple, based upon a given conditional state, either allow the loop to continue, or force it to terminate immediately. That sounds like an intentional part of the logic, not a forced change of it. They do affect the flow, though.

1 Like

for (let i = 0; i < rapperArray.length; i++){
console.log(rapperArray[i]);

console.log(“And if you don’t know, now you know.”);

^^

Hey guys, the exercise marked me as failed too. Just wondering what am I doing wrong here? These are my code.

const rapperArray = ["Lil' Kim", "Jay-Z", "Notorious B.I.G.", "Tupac"];

// Write your code below
for (let i = 0; i < 4; i++) {
  console.log(rapperArray[i]);
}

is this an intermediate step, or is the conditional missing from the code?

2 Likes

Hello there @mtf, this code is just for the first step asking us to create a for loop logging the elements from rapperArray.

1 Like

Perhaps the SCT is looking for the name in the loop parameters…

for (let i = 0; i < rapperArray.length; i++) {

}
6 Likes

That is precisely what it’s looking for… it was careless of me to miss that. Thank you so much @mtf! :pray::pray:

2 Likes