Object vs array
Both objects and arrays in JavaScript are data structures, but they have a different purpose and way of storing information.
In short
| Property | Object | Array |
|---|---|---|
| What it stores | Key -> value pairs | An ordered list of elements by index |
| Keys | Strings or Symbol | Automatic numeric indexes |
| Order of elements | Not guaranteed | Guaranteed |
| Structure type | Associative | Sequential |
| Suited for | Describing entities (a user, a product, etc.) | Lists, collections, data sets |
| Methods | Object.keys(), Object.values(), etc. | map(), filter(), push(), pop(), etc. |
| Type check | typeof obj === 'object' | Array.isArray(arr) |
Example of an object
javascript
const user = {
name: 'Bohdan',
age: 25,
city: 'Kyiv'
};
console.log(user.name); // "Bohdan"Here the keys are strings ("name", "age", "city")
and the order of properties does not matter.
Example of an array
javascript
const numbers = [10, 20, 30];
console.log(numbers[0]); // 10Here the elements are ordered:
0 → 10, 1 → 20, 2 → 30.
Key differences
1. Indexing
- In an array, indexes are numeric and go in order.
- In an object, keys are arbitrary strings.
javascript
const obj = { a: 1, b: 2 };
const arr = [1, 2];
console.log(obj['a']); // 1
console.log(arr[0]); // 12. Order
An array guarantees the order of elements, while in an object the order of properties is not always important (although in modern JS it is preserved by insertion order).
3. Methods
Arrays have special methods for working with collections:
javascript
const arr = [1, 2, 3];
console.log(arr.map(x => x * 2)); // [2, 4, 6]Objects do not, but they can be converted into an array:
javascript
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj)); // [1, 2, 3]4. Purpose
| Type | Used for |
|---|---|
| Object | Structured data (a user, an order, settings) |
| Array | Sequences (product lists, messages, numbers) |
5. Type check
javascript
const arr = [1, 2, 3];
const obj = { a: 1 };
console.log(typeof arr); // "object" (!) - technically an array is also an object
console.log(Array.isArray(arr)); // trueIn JavaScript an array is a special case of an object, just with additional properties and behavior.
SUMMARY
| Criterion | Object | Array |
|---|---|---|
| Storage | "key -> value" | indexed elements |
| Keys | strings / symbols | numbers |
| Order | not guaranteed | preserved |
| Methods | Object.* | map, filter, push, forEach |
| Ideal for | describing entities | working with lists |
| Type check | typeof obj === 'object' | Array.isArray(arr) |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.