Arrays and Array Methods
Which built-in array methods mutate in place versus return a new array, and when to reach for `map`/`filter`/`reduce` instead of a loop.
Arrays and Array Methods
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- An array is a specialized object whose keys are numeric indices and that carries a `length` property the engine keeps in sync automatically — it isn't a separate kind of thing from an object, just one shaped for ordered, indexed data.
- Some array methods mutate the array in place and return something other than the new array itself (`push`, `pop`, `splice`, `sort`, `reverse`, `unshift`, `shift`); others leave the original untouched and return a brand-new array (`map`, `filter`, `slice`, `concat`) — mixing the two up is one of the most common sources of a bug that only shows up somewhere far from where you introduced it.
- `map` transforms every element into something else, `filter` keeps only the elements that pass a test, and `reduce` folds the whole array down into a single value — together they cover the vast majority of "do something to each item" problems without a hand-written loop.
- Reaching for `map`/`filter`/`reduce` over a `for` loop is about intent, not performance — a chain of them tells the next reader *what* is happening to the data at a glance, where a loop makes them trace the accumulator by hand to find out.
- `forEach` looks like `map`, but it returns `undefined` and exists purely to run a side effect per element — using it where you meant `map` is a common tell that someone doesn't yet have the mutating/non-mutating distinction cleanly separated in their head.
Arrays are objects wearing a specific outfit
An array doesn’t need its own chapter because it’s a different kind of
value from an object — it’s the same kind of value, specialized for one
job. typeof [] returns "object" for a reason: an array is an object
whose property keys happen to be sequential numbers (0, 1, 2, …)
and which carries a length property the engine updates automatically
whenever those numeric keys change. Push a value onto ["a", "b"] and
length becomes 3 without you touching it; that bookkeeping, plus a long
list of purpose-built methods, is the entire difference between an array
and an object you happened to name your keys 0, 1, 2.
That framing matters because it explains why arrays get a whole vocabulary of methods that plain objects don’t: once you know your data is ordered and indexed, you can build dozens of useful operations — insert here, remove there, transform every item, fold them into one value — on top of that one guarantee. Objects, lacking order, can’t offer the same menu.
The split that matters more than any single method name
Before learning what each array method does, it’s worth burning one fact into memory: every array method falls into one of two camps, and mixing them up is where most array bugs come from.
Mutating methods change the array you called them on, in place, and
often return something other than the new array — push returns the new
length, splice returns the removed elements, sort and reverse
return the same array reference, now rearranged. Non-mutating methods
leave the original array completely untouched and return a fresh array (or
value) built from it — map, filter, slice, concat.
const original = [3, 1, 2];
const sorted = original.sort();
console.log(original); // [1, 2, 3] — mutated! not what you might expect
console.log(sorted === original); // true — same array, same reference
const doubled = original.map(n => n * 2);
console.log(original); // [1, 2, 3] — untouched
console.log(doubled); // [2, 4, 6] — a brand-new array
The embedded video walks through exactly this split visually — splice,
push, unshift, and pop on one side mutating the array as it goes, and
map, slice, and filter on the other side producing a new array each
time — which is worth watching once you’ve seen the code above, since
watching the array actually change shape (or not) in real time makes the
distinction stick in a way reading about it doesn’t.
map, filter, reduce, and friends
Once you’re reliably telling mutating and non-mutating methods apart, the next layer is the small set of methods that do almost all the real work in day-to-day code:
maptakes every element and transforms it into something else, one-to-one. Given[1, 2, 3],map(n => n * 2)gives you[2, 4, 6]— same length, every item changed.filterkeeps only the elements that pass a test, dropping the rest.[1, 2, 3, 4].filter(n => n % 2 === 0)gives you[2, 4].reducefolds the entire array down into one value — a sum, an object, another array, anything — by running an accumulator through every element.[1, 2, 3].reduce((sum, n) => sum + n, 0)gives you6.find,some, andeveryanswer narrower questions: the first matching element, whether any element matches, and whether all elements match.
These aren’t just shorter syntax for a loop — they’re a different way of
describing the problem. “Filter the completed orders, then map each one to
its total, then reduce those into a grand total” reads like the actual
business rule. The equivalent for loop would bury that same logic inside
one shared accumulator variable, forcing anyone reading it to simulate the
loop in their head to recover what the first version states up front.
Where forEach actually fits
It’s easy to reach for forEach out of habit because it looks like map —
same signature, same callback-per-element shape. The difference is what
comes back: forEach always returns undefined. It exists for side
effects — logging each item, pushing into some external array, firing a
network call per element — not for building a result. If you find yourself
writing const results = []; arr.forEach(x => results.push(f(x))), that’s
arr.map(f) wearing a disguise.
Where this fits in the path
This lesson follows directly from how objects and property descriptors
work, since an array is one of those objects, just with the numeric-key
convention and the array methods layered on top. From here, the array
methods you’ve just learned are the foundation for the next two lessons:
destructuring and spread syntax give you compact ways to pull values out
of arrays and build new ones, and the iterator protocol — which every one
of these array methods relies on internally — explains how something
like for...of or the spread operator actually walks an array element by
element.
Check yourself
Why is it misleading to think of a JavaScript array as a fundamentally different data type from a plain object?
Because under the hood it is an object — one where the engine expects numeric-looking keys and automatically maintains a `length` property as those keys change. Arrays get special treatment and methods because of that shape, not because they're a separate primitive kind of value.
You call `numbers.sort()` and then keep using the original `numbers` variable elsewhere in the function, assuming it's still in its original order. What's gone wrong?
`sort` mutates the array in place and returns the same array, so `numbers` itself is now sorted — there was never an untouched "original" to come back to. If the original order needs to survive, you have to copy the array first, e.g. `[...numbers].sort()`, or use a method that doesn't mutate.
What's the actual difference between `map` and `forEach`, given that both take a callback and run it once per element?
`map` collects each callback's return value into a brand-new array and hands that back to you; `forEach` discards whatever the callback returns and always gives back `undefined`. `forEach` is for side effects (logging, pushing into some other structure); `map` is for producing a transformed array you intend to use.
Why would you choose `.map().filter()` over writing a single `for` loop that does the same transformation and filtering?
Performance is rarely the reason — a well-written loop can be just as fast. The real reason is readability: each method name documents its own single job, so someone scanning the chain can tell what's happening to the data without mentally simulating the loop's accumulator and index variables.
Name two array methods that mutate the array in place, and two that return a new array without touching the original?
Mutating: `push`, `pop`, `splice`, `sort`, `reverse`, `unshift`, and `shift` all change the original array. Non-mutating: `map`, `filter`, `slice`, and `concat` all return a new array and leave the original exactly as it was.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.