Top 5 Data Structures in JavaScript
Let’s go through the Top 5 Data Structures in JavaScript — with explanations, use cases, and examples 👇
🧩 1️⃣ Array
🔹 What it is:
An ordered collection of items — used for lists, stacks, queues, and heaps (via logic).
🔹 Key features:
- Indexed (starts from
0) - Can store any data type (even mixed)
- Dynamic — grows/shrinks automatically
🔹 Example:
let numbers = [10, 20, 30];
numbers.push(40); // Add at end
numbers.unshift(5); // Add at start
numbers.pop(); // Remove from end
console.log(numbers); // [5, 10, 20, 30]
🔹 Use cases:
- Sorting and searching
- Stack/Queue implementations
- Storing ordered lists (e.g., scores, logs)
🪣 2️⃣ Object
🔹 What it is:
A key-value store — like a dictionary or hashmap.
🔹 Key features:
- Unordered (until ES6, insertion order now mostly preserved)
- Keys are strings or symbols
- Very fast lookups
🔹 Example:
let person = {
name: "Alice",
age: 25,
city: "Sydney"
};
console.log(person.name); // Alice
person.job = "Engineer"; // Add new key
delete person.city; // Remove key
🔹 Use cases:
- Representing structured data (like JSON)
- Storing configuration or mapping
- Caching key-value data
🗺️ 3️⃣ Map
🔹 What it is:
A modern key-value store introduced in ES6 — similar to an object, but with more power.
🔹 Key features:
- Keys can be any type (objects, numbers, functions)
- Maintains insertion order
- Has built-in iteration methods
🔹 Example:
let scores = new Map();
scores.set('Alice', 90);
scores.set('Bob', 85);
scores.set({id: 1}, 70);
console.log(scores.get('Alice')); // 90
console.log(scores.size); // 3
🔹 Use cases:
- Caches or lookup tables
- Associating metadata with objects
- When you need guaranteed key order
🧮 4️⃣ Set
🔹 What it is:
A collection of unique values — no duplicates allowed.
🔹 Key features:
- Automatically removes duplicates
- Supports fast membership checking
🔹 Example:
let numbers = new Set([1, 2, 3, 3, 4]);
numbers.add(5);
numbers.delete(2);
console.log(numbers); // Set {1, 3, 4, 5}
console.log(numbers.has(3)); // true
🔹 Use cases:
-
Removing duplicates from arrays
let unique = [...new Set([1, 2, 2, 3, 3])]; // [1, 2, 3] -
Tracking visited nodes in graphs
-
Membership or uniqueness checks
🪵 5️⃣ Stack / Queue (via Array)
🔹 What it is:
Arrays can simulate both these linear data structures easily.
Stack (LIFO):
let stack = [];
stack.push(10);
stack.push(20);
stack.pop();
console.log(stack); // [10]
Queue (FIFO):
let queue = [];
queue.push(10);
queue.push(20);
queue.shift();
console.log(queue); // [20]
🔹 Use cases:
- Function call management
- Undo/Redo operations
- BFS/DFS in graph traversal
💡 Summary
| Data Structure | Syntax | Best For |
|---|---|---|
| Array | [] | Ordered lists, stacks, queues |
| Object | {} | General key-value pairs |
| Map | new Map() | Complex keys, guaranteed order |
| Set | new Set() | Unique collections |
| Stack/Queue | via Array | Algorithmic tasks |
Published on: Oct 19, 2025, 10:47 PM