Not sure how to explain this. I'll try it with an example.
For example, I use the following function as a short for a for
loop over an array:
function forEach(array, f) {
var len = array.length, i;
for (i = 0; i < len; i += 1) {
f(i, array[i], len);
}
};
I guess this is not too uncommon. But it leads to problems when replacing the following for
loop within a function:
function test(someArray) {
for (var i = 0; i < someArray.length; i++) {
if (someArray[i] === 23) {
return true;
}
}
return false;
}
The replacement:
function test(someArray) {
forEach(someArray, function(i, value) {
if (value === 23) {
return true;
}
});
return false;
}
Does not return the function test
when 23 is reached and returns true
but keeps executing the statements after the forEach
and returns false
.