Posted on

Javascript Array.forEach

A quick tip, while it’s fresh on my mind:

If you are writing Javascript for Firefox or XULRunner—or anything supporting Javascript 1.6 technically—then you don’t need to write loops like

for (var i = 0; i < some.object.length; i++) {
    var item = some.object[i];

Instead use the generic Array.forEach() function and save yourself from creating a temporary variable. The above is more cleanly written as

Array.forEach( some.object, function(item) {

This works for any object which has the length property.