What are Array-like Objects?
Introduction
Welcome to today’s blog! If you’ve ever dabbled in JavaScript, you might have stumbled upon the term “Array-like objects.” In this post, we will explore what these are and how they differ from regular arrays, and give practical examples of how to work with them. So, grab your favourite coding beverage, and let’s dive in!
What are array-like objects?
Array-like objects in JavaScript share a few similarities with arrays but don’t have all the capabilities of true arrays. They have a “length” property and can be indexed numerically, giving them some array-like behaviour.
Here’s a simple example of array-like objects for clarity:
In the example above, both “realArray” and “arrayLike” have a “length” property, which suggests they resemble arrays. But the subtle differences matter.
How to identify array-like objects
To determine whether an object is truly an array, JavaScript offers a handy method called `Array.isArray()`. Let’s see this in action:
In this case, we can see that while `realArray` is indeed an array, `arrayLike` is not.
Key features of array-like objects
1. Numeric indices: They have numeric keys (0, 1, 2, etc.)
2. Length property: They possess a `length` property.
3. Bracket notation: You can access elements using bracket notation like `obj[0]`.
4. Not iterable: They cannot be directly iterated over with methods such as `forEach`.
Common examples of array-like objects
You might encounter array-like objects in various places as a developer. Here are a few common examples:
1. The arguments object
Within functions, you can access a special `arguments` object that holds all arguments passed to that function. Here’s how you can use it:
2. HTMLCollection object
Get Godfrey Ouma’s stories in your inbox
Join Medium for free to get updates from this writer.
If you’re working in a browser environment, methods like “getElementsByTagName” return HTMLCollections, which are array-like objects.
3. NodeList object
If you’re working in a browser environment, methods like “querySelectorAll” return NodeLists, which are array-like objects.
Differences between arrays and array-like objects
A significant distinction lies in the fact that array-like objects inherit from `Object.prototype`, while true arrays inherit from `Array.prototype`. This means array-like objects lack access to useful array methods like `forEach()`, `map()`, `filter()`, and so forth.
Here’s a demonstration:
Converting array-like objects to real arrays
Now, you might be wondering how to convert these array-like objects into actual arrays to leverage those powerful array methods. Here are a few methods you can use:
Method 1: Using Array.from()
Method 2: The For-Loop
Method 3: Using Object.keys()
Method 4: Array.prototype.slice
Conclusion
In conclusion, array-like objects are fascinating structures in JavaScript that behave similarly to arrays but have their specific traits. While they can be useful, it’s crucial to know how to convert them into true arrays when you need that extra functionality.
Feel free to experiment with the examples shared, and as always, happy coding! If you have any questions or thoughts, don’t hesitate to drop a comment below!








