That's basically a special case of each:
function each(a, f) {
a = a || [];
for (var i=0; i < a.length; ++i) {
f(a[i]);
}
}
though each is usually implemented as a method of Array, along with map, filter and reduce/fold:
Array.prototype.each = function(f) {
for (var i=0; i < this.length; ++i) {
f(this[i], i);
}
};
Array.prototype.map = function(f) {
var result = [];
for (var i=0; i < this.length; ++i) {
result[i] = f(this[i], i);
}
return result;
};
Array.prototype.map = function(keep) {
var result = [];
for (var i=0; i < this.length; ++i) {
if (keep(this[i], i)) {
result.push(this[i]);
}
}
return result;
};
Array.prototype.foldl = function(f) {
var result = this[0];
for (var i=1; i < this.length; ++i) {
result = f(result, this[i]);
}
return result;
};
Array.prototype.foldr = function(f) {
var i=this.length-1
var result = this[i];
for (--i; i >= 0; --i) {
result = f(this[i], result);
}
return result;
};
Array.prototype.reduce = Array.prototype.foldl;
Note that using for (... in ...) with arrays can cause problems as it will pick up properties defined on an array in addition to the integer indices.
new Array; just doing[]works the same and is way simpler.