map, filter, reduce
map, filter, reduce in Modern JavaScript
Functional array processing is one of the most transformative paradigms in modern JavaScript. Rather than writing imperative for loops with mutating variables, map, filter, and reduce express data transformations declaratively, producing cleaner, bug-free, and highly testable code.
1. Array.prototype.map()
map() creates a new array populated with the results of calling a provided function on every element in the calling array. It never mutates the original array and always maintains a 1:1 length ratio.
2. Array.prototype.filter()
filter() creates a shallow copy of a portion of a given array, filtered down to just the elements that pass the test implemented by the provided callback function (evaluates to truthy).
3. Array.prototype.reduce()
reduce() executes a user-supplied "reducer" callback function on each element of the array, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements is a single value (number, string, object, or aggregated array).
Computing Aggregates (Sum)
Grouping Data with reduce()
4. Method Chaining: The Declarative Pipeline
Because map and filter return new arrays, you can chain them into functional data pipelines:
Practice Quiz
find, some, every
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Form Events and Validation | find, some, every |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.