DEV Community

yk-david
yk-david

Posted on

What if Array is Object like thing after all?

Both Array and Object are a JavaScript data structure. While Array stores data with sequential index number, Object attaches values to key.

But couldn't we consider that index of Array is a hidden equivalent to Object's key? (of type number)

Let me develop:

// array
fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[0]); // 'apple' ๐Ÿค”

// object
fruits = {0: 'apple', 1: 'banana', 2: 'cherry'};
console.log(fruits[0]); // 'apple' ๐Ÿ˜ฎ

Hm... Could Array potentially be build on Object?

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

Correct

console.log(Array instanceof Object) // true
console.log([] instanceof Object) // true
Enter fullscreen mode Exit fullscreen mode