HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›Array Methods (map / filter / reduce)

Array Methods (map / filter / reduce)

Transforming (map), filtering (filter), and reducing arrays to a single value (reduce) — the heart of modern JS.

Simple enough for a 5-year-old to understand

map: transform

Applies an operation to each element and produces a NEW array. Does not modify the original.

Example code
1
const prices = [100, 200];
Array.
DataArray.
2
const withVat = prices.map(f => f * 1.2);
Add 20% to each price.
CodeAdd 20% to each price.
3
console.log(withVat);
[ 120, 240 ]
Output / Input[ 120, 240 ]
DataCodeOutput / Input

filter: filter

Example code
1
const numbers = [1, 2, 3, 4];
Array.
DataArray.
2
const evens = numbers.filter(x => x % 2 === 0);
Take only the evens.
CodeTake only the evens.
3
console.log(evens);
[ 2, 4 ]
Output / Input[ 2, 4 ]
DataCodeOutput / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Select and print ONLY even numbers from the [1, 2, 3, 4] array using filter.

🎯 Expected output: [ 2, 4 ]

Loading editor…

About this topic

Array methods are among the most used tools in modern JavaScript. You transform every element with map, filter them with reduce, or reduce them to a single value. This trio reduces a large part of the code you would write with loops down to a single line and clearly shows your intent.

The most important feature of these methods is that they DO NOT mutate the original array: they return a new array. This keeps your code predictable — when you pass an array to a function, you know it won't break it. In contrast, some methods like sort and reverse mutate the array in place, and this asymmetry frequently produces bugs.

Methods can be chained: you can filter first and then transform. Chaining increases readability, but since each link iterates over the array once more, you need to consider performance on very large arrays.

What you will learn

  • transforming with map, filtering with filter
  • generating totals and groupings with reduce
  • searching and checking with find, some, and every
  • sorting with sort and using a comparison function
  • Which methods mutate the array in place
  • Chaining methods

When to use?

To display the record list coming from the server on the screen, map is used; to select those matching a specific condition, filter; and to calculate the total amount, reduce. To find a single record, find; to check if at least one matches, some.

When sorting, providing a comparison function is almost always necessary; the default behavior sorts numbers like text.

Common mistakes

Wrong

const nums = [10, 9, 100, 2];
nums.sort();
console.log(nums);

By default, sort converts elements to TEXT and sorts them alphabetically; the result becomes 10, 100, 2, 9. You need to provide a comparison function for numerical sorting.

Correct

const nums = [10, 9, 100, 2];
nums.sort((a, b) => a - b);
console.log(nums);

Wrong

const prices = [10, 20];
const doubled = prices.map((p) => {
  p * 2;
});
console.log(doubled);

When curly braces are used, return is mandatory; since it's omitted, every element becomes undefined and the result is an array of undefineds. Leaving curly braces out on single-line transformations is the safest bet.

Correct

const prices = [10, 20];
const doubled = prices.map((p) => p * 2);
console.log(doubled);

Wrong

const items = [1, 2, 3];
const sorted = items.sort((a, b) => b - a);
console.log(items);

sort sorts the array IN PLACE and returns the same array; items becomes sorted as well. If you want to preserve the original order, you must copy it first.

Correct

const items = [1, 2, 3];
const sorted = [...items].sort((a, b) => b - a);
console.log(items);

Frequently asked questions

How does reduce work?

It carries an accumulator value while iterating over the array: at each step, it takes the result so far and the current element, and returns the new result. It is used to calculate totals, group by a field, or convert an array into an object. Providing an initial value is important; otherwise, you will get an error on an empty array.

What is the difference between find and filter?

find returns the FIRST element that matches the condition, or undefined if not found. filter returns ALL matching elements as a new array, or an empty array if none are found. If you are looking for a single record, using find is both faster and more accurate.

Which methods mutate the array?

sort, reverse, splice, push, pop, shift, unshift, and fill mutate the array in place. map, filter, slice, concat, and reduce do not mutate it, and return a new value. Knowing this distinction prevents unexpected side effects; if in doubt, copy with the spread operator first.

Summary

The trio of map, filter, and reduce are the core tools of modern JavaScript and do not mutate the original array. The two most frequent pitfalls: sort sorting like text by default and mutating the array in place.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code