Javascript quirks, tips, et cetera

Slice on array-like objects

MDN: Array.prototype.slice for Array-like objects

function incorrect() {
    return arguments.slice(1); //throws and error
}

function error() {
    return Array.prototype.slice(arguments, 1);
}

The incorrect function does not work because arguments is not an array - it is an Array-like object. However, we can still use the slice method from Array‘s prototype.

Copyright © 2008-present Brendan Graetz