Real Ultimate Programming

The Home for People Who Like to Flip Out and Write Code

JavaScript Idioms Revisited

In an earlier post about JavaScript idioms, I talked about a common idiom for copying an array:

1
2
3
4
    var myArray = [];
    for (var i = 0; i < source.length; i++) {
        myArray[i] = source[i];
    }

In a comment, Jesse offers the following alternative:

1
    var args = Array.prototype.slice.apply(arguments);

To my eye, this is clearly preferable, and I hope more people use it in the future. Thanks for pointing it out, Jesse.

Also: here’s a good list of JavaScript idioms.

Back to flipping out…