map
Transforms every item into a new item.
map, filter and reduce are the core transformation methods for arrays. They let you describe what result you want from list data.
const scores = [72, 88, 94];
const highScores = scores.filter((score) => score >= 80);
const labels = highScores.map((score) => `${score}%`);
Arrays & Collections
Use map when every item becomes a new item. The result array has the same length as the original array.
Use filter when only some items should remain. The result array can be shorter than the original array.
Use reduce when the array becomes one final value, such as a total, object, grouped structure or summary.
Transforms every item into a new item.
Keeps items that pass a condition.
Combines all items into one result.
Methods can be chained when each step stays readable.
Examples
const activeTitles = records .filter((record) => record.active) .map((record) => record.title);
records.map((record) => {
list.append(renderRecord(record));
});
// forEach communicates side effects better.
Code patterns
These are the patterns developers search for constantly: reading items, copying arrays, choosing map/filter/reduce, sorting with a comparator, destructuring and using Set or Map.
Transform item shape.
const names = ["html", "css"]; const labels = names.map((name) => name.toUpperCase());
Select matching items.
const scores = [72, 88, 94]; const passed = scores.filter((score) => score >= 80);
Combine numbers into one value.
const scores = [72, 88, 94]; const total = scores.reduce((sum, score) => sum + score, 0);
Keep each step short and named when needed.
const labels = records .filter((record) => record.active) .map((record) => record.title);
Rules that matter
Arrays and collections are not just storage. They describe whether the code is selecting, transforming, grouping, counting, sorting or enforcing uniqueness.
Every input item should produce one output item.
Return true to keep an item.
Totals, grouped objects and summaries fit reduce.
If a loop is clearer, use a loop.
Extract predicates and mappers when chains grow.
Use forEach or a loop when you do not need the returned array.
Production thinking
These methods let data transformation read like intent instead of manual index management.
Filtering and mapping rendered lists should preserve meaningful text, order and states for users.
Production transformations should stay readable and avoid chains that hide too many rules at once.
Content pipelines should keep rendered lists complete and deterministic.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
A collection method should tell the reader what kind of operation is happening before they read the callback body.