Higher-Order Functions — Map, Filter, Reduce
Difficulty: medium
Overview
Higher-order functions accept or return other functions. Array.prototype.map returns a new array of transformed elements. Array.prototype.filter returns a new array of elements that pass a predicate. Array.prototype.reduce accumulates elements into a single value using a callback and optional initial accumulator. None of them mutate the original array. They can be chained for expressive data pipelines.
Practice Linked Questions
Q1. What does `Array.prototype.map()` return?
Select one answer before revealing.
Q2. What is the output? ```js const evens = [1,2,3,4,5].filter(n => n % 2 === 0); console.log(evens); ```
Select one answer before revealing.
Q3. What is the output? ```js const total = [1, 2, 3, 4].reduce((acc, val) => acc + val, 0); console.log(total); ```
Select one answer before revealing.
Q4. What is the difference between `map()` and `forEach()`?
Select one answer before revealing.
Q5. What is the output? ```js const doubled = [1, 2, 3].map(n => n * 2); console.log(doubled); ```
Select one answer before revealing.
Q6. What does `Array.prototype.reduce()` use as its second argument?
Select one answer before revealing.