Skip to main content

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

PropertyObjectArray
What it storesKey -> value pairsAn ordered list of elements by index
KeysStrings or SymbolAutomatic numeric indexes
Order of elementsNot guaranteedGuaranteed
Structure typeAssociativeSequential
Suited forDescribing entities (a user, a product, etc.)Lists, collections, data sets
MethodsObject.keys(), Object.values(), etc.map(), filter(), push(), pop(), etc.
Type checktypeof 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]); // 10

Here 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]); // 1

2. 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

TypeUsed for
ObjectStructured data (a user, an order, settings)
ArraySequences (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)); // true

In JavaScript an array is a special case of an object, just with additional properties and behavior.


SUMMARY

CriterionObjectArray
Storage"key -> value"indexed elements
Keysstrings / symbolsnumbers
Ordernot guaranteedpreserved
MethodsObject.*map, filter, push, forEach
Ideal fordescribing entitiesworking with lists
Type checktypeof obj === 'object'Array.isArray(arr)

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.