JS: Difference Between Array vs Array-Like Object

By Xah Lee. Date: .

Difference between array and array-like object

// create a array-like object
let arLike = { 0: "a", 1: "b", length: 2 };
arLike[2] = 99;

console.log(arLike.length === 2);
// length would be 3 if it's a true array

console.log(Array.isArray(arLike) === false);

console.log(Reflect.getPrototypeOf(arLike) === Object.prototype);

// ---------------
// real array
let trueArray = ["a", "b"];
trueArray[2] = 99;

console.log(trueArray.length === 3);

console.log(Array.isArray(trueArray));

console.log(Reflect.getPrototypeOf(trueArray) === Array.prototype);

JavaScript, Array

BUY Ξ£JS JavaScript in Depth