JS: Array-Like Object

By Xah Lee. Date: . Last updated: .

What is a array-like object

A object such that is all of the following:

  1. has property names of consecutive non-negative integers {"0", "1", "2", …}.
  2. has a property "length" with a value that's the number of these properties.
  3. is not a true array. That is, Array.isArray( obj) returns false.

[see Understand JS Array]

Difference between array and array-like object

// create a array-like object
let a1 = {0:"a", 1:"b" , length:2};
a1[2] = 99;
console.log(a1.length); // 2
// length would be 3 if it's a true array

// real array
let a2 = ["a", "b"];
a2[2] = 99;
console.log(a2.length); // 3

[see Prototype and Inheritance]

[see Array.prototype]

Check if a object is true array

Array.isArray( obj)

[see Array.isArray]

Where does array-like object came from

In DOM, method such as document.getElementsByClassName return array-like objects.

function arguments Object is a array-like object.

Convert array-like object to array

Array-Like Object to Array

JavaScript Array

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc