Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops Working with 'for' Loops Exit a Loop

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

break

If the break keyword is in the If statement it should only exit from the if statement how come it's exiting us from the loop, as we did i=not give any reference to the loop?

Swan The Human
seal-mask
.a{fill-rule:evenodd;}techdegree
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points

The break keyword stops whatever loop is running directly where it is and exits whatever loop or statement you are in. At that time it will go directly to the next statement in your code. Depending where you put the break, you could be stopping the loop before it executes the code you want it to.

I would have to see your code specifically, but maybe try experimenting with where your "break" call is in your code.

If you can, post the code in question and that would help me understand your question more!

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Hello Sean, I actually was not writing any code, just wanted to lighten up that what actually break; keyword do.

Thanks to your explanation, I almost get it,

Just one more thing as you said it directly effects the loop right so that means it wherever I put this break; keyword inside the Loop, once the JavaScript engine reaches the break; keyword it will just exit the loop not just it's parent code? Am I right?

3 Answers

Swan The Human
seal-mask
.a{fill-rule:evenodd;}techdegree
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points

If I am understanding what you are saying correctly, then yes, you are right. Say you had more then one loop inside of a function. If you put the break statement in the first one and it was activated, it would automatically stop that loop in its tracks and move on to the immediate next step in the function. I hope this helps!

Steven Parker
Steven Parker
229,744 Points

As Swan pointed out, a break exits the innermost loop, not the conditional itself.

And yes, it only exits that loop, but continues the code outside. For example:

for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j == 1) break;
  }
  console.log("test");
}

This code would still log "test" 3 times.

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Hi Steven, so as per your code it will go like

i = 0 j = 0

i = 1 j = 1

i = 2 j = 2

is that correct ? so the break will exit from the loop but over here it will only exit through the Parent Loop right?

Steven Parker
Steven Parker
229,744 Points

Close, but j will never get past 1 because of the break. So it goes: i=0 j=0, j=1 i=1 j=0, j=1 i=2 j=0, j=1.