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 Introducing ES2015 Objects and New Collection Types For...Of

Harsh Khandelwal
Harsh Khandelwal
5,539 Points

Why is the break keyword illegal statement error when I try to write break in the if condition of the forEach code?

teachers.forEach(teacher => {
  console.log(teacher.name);
  if (teacher.name === 'Nick') {  
    console.log(teacher.rating);
  break;
  }
});

break;
^^^^^
SyntaxError: Illegal break statement

christopher abramson
christopher abramson
22,741 Points

I don't believe you can use forEach like that. If you want to achieve the same result with a break you could use a

Switch

or if you want to do it without a break you could use a

While loop.

Mark Casavantes
Mark Casavantes
Courses Plus Student 13,401 Points

Hello Harsh,

It would be helpful to print out all your code when you have a question.

From what I can see I would try switching => to >=.

http://www.w3schools.com/js/js_comparisons.asp

I hope this is helpful.

formatted code

1 Answer