What are
Data Structures?
Every program ever written was built on decisions about how to organise data. Those decisions — choosing the right structure for the job — are what separate engineers who write fast software from those who don't.
What is a data structure?
A data structure is a way of organising data in memory so that specific operations are fast. An array gives instant access by index, a hash map gives instant lookup by key, and a tree keeps data sorted for fast search. Choosing the right structure is usually the difference between code that takes milliseconds and code that takes minutes.
const list = [3, 1, 4]; // array: index access O(1)
const prices = new Map([["tea", 2]]); // hash map: key lookup O(1)
const seen = new Set(["a", "b"]); // set: membership test O(1)
const stack = []; stack.push(1); stack.pop(); // LIFO
const queue = []; queue.push(1); queue.shift(); // FIFOThe core idea
A data structure is a specific way of organising data in memory so that common operations — access, insert, delete, search — can be performed efficiently.
Think of it like physical storage. A numbered bookshelf lets you retrieve book #5 in O(1). A pile on the floor means scanning every book — O(n). The data is identical. The organisation is everything.
Numbered Shelf (Array)
Every slot is numbered. Reach position 5 instantly. Insert in the middle and every item after it must shift.
Access: O(1) · Insert-middle: O(n)Labelled Cabinet (Hash Map)
Each drawer is labelled. Look up "alice" and the label points you directly to the right drawer — no searching required.
Lookup: O(1) avg · No orderingLIFO Pile (Stack)
You can only take from or add to the top. Last item added is first removed. Perfect for undo history and recursion.
Push/Pop: O(1) · Random access: noneBranching Hierarchy (Tree)
Branching hierarchy. Following branches finds any node. Balanced trees keep every path short — O(log n).
Search: O(log n) in a balanced treeWhy the right choice matters
The difference between a good and bad data structure choice isn't academic. It directly translates to user experience, infrastructure cost, and whether your system scales to 10 users or 10 million.
scenario: find a user by ID in a list of 1,000,000 users
The four families
Every data structure belongs to one of four broad families. Understanding the family immediately tells you the core trade-offs.
Linear
Elements in a sequence. Iteration is natural. Random access varies by implementation.
Tree-based
Hierarchical branching. Balanced trees give O(log n) search, insert, and delete.
Hash-based
Trade a little memory for speed. O(1) average for lookup. No inherent ordering.
Graph-based
Model arbitrary relationships between nodes. Foundation of routing, scheduling, dependency resolution.
Abstract type vs. concrete structure
This distinction trips beginners constantly. A Stack is an abstract data type (ADT) — a contract that specifies push, pop, and peek. The concrete structure is the internal implementation. You can back a stack with an array or a linked list and the interface stays identical.
1ADT Stack:2 push(item) → add item to top3 pop() → remove and return top item4 peek() → return top item without removing5 isEmpty() → return true if no items67// Two valid concrete implementations:8Array-backed Stack → contiguous block of memory9Linked-list Stack → chain of nodes connected by pointers1011// Same contract. Very different memory layouts.
Abstract Data Types
Define the contract — the interface you code against.
Concrete Implementations
Specify memory layout and algorithm detail.
How to choose the right structure
Ask these three questions in order. The answers narrow the field quickly.
What operations do I need most?
Fast key lookup points to a hash map. Ordered traversal or range queries point to a BST. LIFO / FIFO semantics point to a stack or queue. Always-available minimum or maximum points to a heap.
Quick Reference — Big O
| Structure | Access | Search | Insert | Delete |
|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) |
| Linked List | O(n) | O(n) | O(1) | O(1) |
| Hash Map | — | O(1)* | O(1)* | O(1)* |
| BST (balanced) | O(log n) | O(log n) | O(log n) | O(log n) |
| Heap | O(n) | O(n) | O(log n) | O(log n) |
* Average case. Worst case with collisions: O(n).
Operation cost, visualised
Each bar represents how fast the cost grows as n increases. You'll study Big O formally in the next chapter.
Constant time
Logarithmic time
Linear time
Linearithmic time
Quadratic time
Exponential time
🎯 Quick Check
You need a leaderboard that always returns the top-10 highest scores instantly, with fast inserts of new scores. Which structure fits best?
What's next?
Data Structures Introduction: summary
Key points
- Every data structure is a trade-off: fast at some operations, slower at others.
- Linear structures (arrays, linked lists, stacks, queues) store items in sequence; non-linear ones (trees, graphs) store relationships.
- Big-O notation describes how an operation's cost grows with the amount of data.
- JavaScript has arrays, objects,
MapandSetbuilt in; the rest you build from them.
Frequently asked questions
›Why should I learn data structures?
They make your programs fast and are the core of most technical interviews. Knowing when to use a Set instead of an array alone can turn an O(n²) loop into O(n).
›Can I learn data structures with JavaScript?
Yes. The concepts are language-independent, and JavaScript lets you run and visualise every example in the browser.
›What should I learn first?
Big-O notation, then arrays, hash maps, stacks and queues, then linked lists, trees and graphs.