Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,740 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
Pro 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