Array-like Objects
An array-like object is any object that has:
- A
lengthproperty. - Indexed elements (i.e., properties with numerical keys).
Array-like objects do not have the standard array methods like push(), forEach(), or map().
Common examples of array-like objects include:
- Arguments object in functions.
- NodeList objects returned by methods like
document.querySelectorAll. - HTMLCollection objects
Example
function example() {
console.log(arguments); // Arguments object, array-like but not a true array
let argsArray = Array.from(arguments);
console.log(argsArray); // Now it's a true array
}
example(1, 2, 3);
// Output:
// Arguments(3) [1, 2, 3, callee: Æ’, Symbol(Symbol.iterator): Æ’]
// [1, 2, 3]