JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – can break statement work on array forEach method

JavaScript Quiz – can break statement work on array forEach method

Break Statement –

The break statement is used to terminate the current running loop, switch or label statement and move the program control to statement after terminated statement.

It’s syntax looks like –

break [label];

For more detail about javascript break statement click here.

Array forEach Method –

Array forEach Method ( Array.forEach() ) is used to iterate the array and it executes the provided function once for every element.

It’s syntax looks like –

arr.forEach(callback(currentValue [, index [, array]])[, thisArg])

For more detail about javascript array foreach method click here.

JavaScript Question

In this javascript quiz, find out can break statement work on javascript array forEach method as break exist the loop.

First we declare items variable using const keyword and assign an javascript array to it.

const items = [1, 2, 3, 4, 5];

In the next step, we use forEach method of array which iterate an array and used on items.

items.forEach((item) => { … })

And we also use a condition inside a forEach method and inside it, we use a break.

if(item > 3){ break; }

Now after it, we check a console log of the item. So, what should be the output of it?

Answer – Error

Click To Run Code


Now, you know the answer is “Error“. But why this happen it should be 1, 2. Let me explain.

So the answer is simple, it’s correct the break stop the iteration but it does not work on the forEach method.

When we use a break statement in forEach statement it gives a “Illegal break statement

At last, That’s why we get an answer “Error“.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – filter array method based on the element size
  2. JS Quiz – copy object with JSON stringify and parse method
  3. JavaScript Quiz – can isFrozen method really check freeze object
  4. JS Quiz – new set object can return the same size


To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
Share via
Copy link