DEV Community

Cover image for JavaScript Revelations: Journeying Through Frontend Interview Preparation (live-stream note)

JavaScript Revelations: Journeying Through Frontend Interview Preparation (live-stream note)

Nastassia Danilava on September 13, 2023

It's not my first job as a frontend developer, but I prepare for EVERY interview because some things have changed, some I need to revise, and some ...
Collapse
 
szalonna profile image
Joe • Edited

You can stop if you throw sg in the callback. Like:

[1, 2, 3, 4, 5, 6].forEach((e) => {
  console.log(e);
  if (e > 2) throw 'foreach stopped':
});
Enter fullscreen mode Exit fullscreen mode

Console output be like:

> 1
> 2
> 3
> Uncaught foreach stopped
Enter fullscreen mode Exit fullscreen mode

Of course you can prepare for this case by wrapping it into a try-catch.

Collapse
 
tymur_minhaziiev profile image
Tymur Minhaziiev

so, how to stop the loop?

Collapse
 
nastassia profile image
Nastassia Danilava • Edited

@tymur_minhaziiev you can use break inside the for. there is no way for forEach or map