I am using forEach() method called from an array in JavaScript. When I write return;
somewhere inside the method which is called for every element in array I return
To quote Mozilla Developer Network:
There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool, use a plain loop instead. If you are testing the array elements for a predicate and need a Boolean return value, you can use every() or some() instead. If available, the new methods find() or findIndex() can be used for early termination upon true predicates as well.
Having said that, I believe your solution to have set a flag to return out of the function is the most appropriate and simple.