JS: Array-Like Object

By Xah Lee. Date: . Last updated: .

What is a array-like object?

A object such that:

  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 JS: Understand JS Array]

What is the 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 JS: Prototype and Inheritance]

[see JS: Array.prototype]

How to check if a object is true array?


[see JS: Array.isArray]

Where does array-like object came from?

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

JavaScript's arguments object is a array-like object.

[see JS: Function's “arguments” Object]

How to convert array-like object to array?

[see JS: Array-Like Object to Array]

JS Array

  1. Understand JS Array
  2. Create Array
  3. Sparse Array
  4. Array-Like Object
  5. Array How-To

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials