What is the best way to implement a simulation of a loop’s break statement, when one does iterate through a user/engine defined function?
forEach([0, 1, 2, 3, 4], function (n) {
console.log(n);
if (n === 2) {
break;
}
});
I’ve thought of implementing forEach in a way that would break when the function returns false. But I would like to hear thoughts on how that is normally done.
returningfalseis the most common way to do it. That’s what jQuery’s iterator function.each()does:And its very simplified implementation: