Whenever I write a loop, I always use closure if it's appropriate by way of a self-executing anonymous function. I will typically write my searching loops as follows:
var i = 0, length = myArr.length,
fe = null;
for (; i < length; i++) {
if ((function (el) {
if (el.searchCondition) {
fe = el;
return true;
}
}(myArr[i])) === true) {
break;
}
}
return fe;
I thought it was minimal code and pretty clean. I know it's not the easiest to read - but is there any reason why I should not be doing it this way?
map
,filter
,reduce
, etc... \$\endgroup\$