JS: Difference Between Array vs Array-Like Object
Difference between array and array-like object
- For true array, value of
length
will automatically change. - For true array, its parent is Array.prototype. γsee Prototype and Inheritanceγ
- For true array, Array.isArray return true.
// 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);