Arrays & Collections
An array in JavaScript is an object with numeric keys and a special length property, not a contiguous block of memory as in C. Two things follow that interviews circle around — an array can be made sparse (with holes), and almost every method either mutates in place or returns a new array, and mixing up those two classes is not allowed.
On top of the array the language gives four key collections — Map and Set with their weak variants WeakMap/WeakSet — and typed arrays for raw bytes. Each solves a problem a plain object or array solves badly — object keys, uniqueness, attaching metadata without memory leaks, binary data. The full map lives in the layers below.
Topic map
- Array methods —
map,filter,reduce,forEach— what each returns and what it is for. - Iteration methods —
some,every,find,findIndex, and their short-circuiting. - Array mutation — methods that change the array in place versus those returning a new one;
sliceversussplice. - Sorting — the default lexicographic order, comparator, stability, and holes.
- Sparse arrays — dense versus sparse, holes, and why methods skip them.
- Typed arrays —
ArrayBufferas raw bytes and views likeUint8Array. - The Map collection — keys of any type, insertion order, and how it differs from an object and a
Set. - The Set collection — unique values,
SameValueZero, and deduplication. - Weak collections —
WeakMap/WeakSet, weak references, and garbage collection.
Common traps
| Mistake | Consequence |
|---|---|
Using forEach where you need a result | forEach returns undefined; build an array only with map |
Assuming sort() sorts numbers | The default sort is lexicographic — [1, 10, 2]; you need a comparator (a, b) => a - b |
Forgetting sort, reverse, splice mutate | The source array changes in place — copy first with [...arr] |
Confusing slice and splice | slice copies and leaves the source alone; splice cuts from the source |
Expecting map to walk holes | Holes are skipped, and map preserves them in the result |
| Using an object as a dictionary with object keys | Object keys are strings/symbols only; for object keys you need a Map |
Expecting WeakMap to iterate or expose size | Weak references can vanish at any moment — enumeration is impossible |
Interview relevance
Arrays and collections are asked as a check of whether you separate mutation from copy, value from reference, and whether you know which collection fits which task. A candidate who answers "how do I dedupe" with [...new Set(arr)] and adds a note about SameValueZero instantly looks stronger.
Typical checks:
- Which methods return a new array versus mutate the source, and what they return.
- How to sort numbers correctly and why
sortwithout a comparator gets it wrong. - How a
Mapdiffers from an object and from aSet, and when to choose each. - What
WeakMap/WeakSetare for and how they relate to garbage collection.
Common wrong answer: "map and forEach are the same" or "sort() handles numbers itself". In fact forEach returns nothing, and sort without a comparator compares strings of code units.