Arrays vs Objects: When to Use Which
The two collection types that show up in every program. Picking the right one for the situation makes everything that comes after easier.
The Decision in One Sentence
Use an array when you have many of the same kind of thing in order. Use an object when you have one thing with many named fields. Use arrays of objects when you have many things, each with named fields - which is most real data.
That sentence handles 90 percent of beginner choices. The full mechanics of each type live on the arrays and objects concept pages.
How to Recognize the Right Choice
If you find yourself naming variables user1, user2, user3 - that is an array trying to escape. Replace with users[0], users[1], users[2].
If you find yourself with userName, userEmail, userAge - that is an object trying to escape. Replace with user.name, user.email, user.age.
If you find yourself with user1Name, user1Email, user2Name, user2Email - that is an array of objects begging to be created.
Worked Examples
Three concrete shopping-cart variations:
// Just IDs (array)
const cart = [123, 456, 789];
// Single product (object)
const product = { id: 123, name: "Tea", price: 4.99 };
// Cart with details (array of objects)
const cart = [
{ id: 123, name: "Tea", price: 4.99, qty: 2 },
{ id: 456, name: "Coffee", price: 6.50, qty: 1 },
];The third form is what every real e-commerce backend uses.
When You Want O(1) Lookup
If you need to answer give me the product with id 123 frequently, an array of objects forces you to scan - O(n). For 10 items it does not matter. For 100,000 items it does.
The fix is keying by ID:
const productsById = {
"123": { name: "Tea", price: 4.99 },
"456": { name: "Coffee", price: 6.50 },
};
productsById["123"].name; // O(1)JavaScript also has Map for non-string keys. Python has dicts. Java has HashMap.
When Both: Pick the Right Top-Level
If you sometimes need to iterate in order (display a list), prefer array of objects. If you almost always need lookup by ID, prefer object-keyed-by-id. If you need both, store both - the small memory cost is worth the readability.
Anti-Patterns Worth Avoiding
- Numeric-string keys when you wanted an array. If keys are 0, 1, 2, 3 in order, you have an array.
- Arrays of arrays for tabular data. Use array of objects (one per row) - 5x more readable.
- Spreading on hot paths. Spread copies the entire array. Fine for 10 items, deadly in a 60fps loop.
- Mutating shared arrays. Caller passes you an array; you push to it; caller is surprised.
For more, see our common array bugs page.
TL;DR Decision Tree
- Many of the same thing, order matters? Array.
- One thing, many named fields? Object.
- Many things with fields each? Array of objects.
- Need fast lookup by key? Object/Map keyed by id.
Practice in the playground. Then take the array and object quizzes.
Combining Arrays and Objects in the Wild
Real applications combine these two structures constantly. A user record might be an object containing an array of order objects, each containing an array of line-item objects. Three layers deep is normal. Five layers deep is a smell - flatten the data before someone has to maintain it.
The general rule: as data gets nested, switch from passing the whole tree around to passing IDs and looking up the related object. Big web apps almost always normalize their state into flat lookup tables keyed by ID, then re-assemble the tree when rendering. Redux's normalization guide covers the canonical pattern.
The JSON Connection
Every API you will ever consume returns JSON, and JSON is just a text representation of arrays and objects. Once you internalize that, parsing API responses stops feeling magical:
// JSON from an API
{
"user": {
"id": 42,
"name": "Aisha",
"orders": [
{ "id": 100, "total": 49.99 },
{ "id": 101, "total": 12.50 }
]
}
}
// In JS, after JSON.parse:
data.user.name // "Aisha"
data.user.orders[0].total // 49.99
data.user.orders.length // 2Object navigation with dot syntax. Array navigation with brackets. Same patterns as the lessons. The web runs on this.
Immutable vs Mutable Updates
Modern JavaScript and Python both encourage immutable updates - returning a new array or object instead of modifying the original. Why? Because shared mutable state causes bugs that are very hard to find. cart.push(item) mutates the cart someone else might be reading. const newCart = [...cart, item] returns a new cart while leaving the old one alone.
For tiny scripts, mutation is fine. For anything with shared state - React, Redux, multi-threaded code - immutability prevents an entire category of bugs. Spend an afternoon learning the spread operator and you will use it the rest of your career.
Where to Go From Here
For the syntax mechanics, see arrays and objects. For the bugs that come from confusing them, see the common-bugs pages: arrays and objects. To practice, build the to-do list project - it uses arrays of objects throughout.
Frequently Asked Questions
Should I use Map or a plain object in JavaScript?
Use Map when keys are not strings, when you need ordered iteration of insertions, or when the size matters and you need a .size property. Use a plain object for everything else - it is shorter and works with JSON.stringify out of the box.
Are sets useful for beginners?
Yes - any time you need uniqueness. new Set([1, 2, 2, 3]) gives you {1, 2, 3}. [...new Set(arr)] dedupes any array. Worth knowing in week two.
What about tuples in Python?
Tuples are immutable arrays. Use them for fixed-size groups (a coordinate pair, a return value with two parts). For lists you mutate, use a list. JavaScript has no native tuple - arrays are flexible enough.
How do I sort an array of objects?
arr.sort((a, b) => a.price - b.price) sorts numerically by price. The comparator receives two items and returns negative/zero/positive. Always pass a comparator for object arrays.