Higher-order functions & array methods
Understand the idea
A list transformation produces instead of a new list. The ran, but the method was chosen for side effects rather than returned values.
Choose the processing station first
A higher-order takes a function as input or returns one. That sounds abstract until you use map: the array method handles visiting the items, and your small callback describes the change for one item. You can focus on the rule instead of rewriting loop bookkeeping.
Transform and select
Pick the method by the result you need. If you want a changed value for every item, choose map. If you want some of the existing items, choose filter. Both return a new array; neither choice guarantees that nested were copied.
A callback can receive the current item, its index, and the original array. You do not have to name you do not need. In the example, price is just the current item. map collects each callback result; filter instead keeps original items whose callback test is .
| Method | What its callback does | What you get back |
|---|---|---|
| map | Returns a replacement for each item | A new array of the same length (for a dense input) |
| filter | Returns whether to keep each item | A new array of selected original items |
| reduce | Returns the next accumulator | One accumulated value; an initial value handles empty input |
| forEach | Performs work for each item | undefined; it does not collect callback results |
| find | Tests for the first match | That item, or undefined |
| some | Tests whether any item matches | A boolean; false for an empty array |
| every | Tests whether all items match | A boolean; true for an empty array |
Find, test, or perform an effect
map returns a new array with one callback result per visited element. filter returns the elements whose callbacks are truthy. forEach performs and returns undefined. When a braced map callback forgets return, each visited position gets undefined, even if a calculation happened inside the callback.
Combine into one result
A running total needs one result that changes as items are processed. That is the accumulator. It can be a number, an object, or another useful result shape; reduce is not limited to addition.
reduce receives an accumulated result and the current item. Return the next accumulated result each time. Supplying zero as the initial value makes a sum work for an empty too. For a complicated transformation, a readable loop is a perfectly good choice; shorter code is useful only when its meaning stays clear.
Keep the callback honest
A callback is still an ordinary function. An expression-bodied arrow returns its expression automatically; adding a block means you must return the result yourself. Separating transformation steps into named intermediate values often makes the callback easier to check.
The example passes three result shapes through the stations: three discounted numbers, then two affordable numbers, then one total. Each step has a name so you can inspect it. These methods do not inherently copy nested objects, and a callback can still mutate one. Also, forEach does not wait for callbacks; use an awaited loop or .all when waiting matters.
Mental model
FOLLOW THE VALUE
The station produces one transformed result per price.
01 / 03Trace the transformation
Live example
Follow the output shape: three prices become three discounted prices, then two selected prices, then one total.
Your console output appears here.
Guided challenge
Return a transformed array
Choose the method that produces one discounted number for each input price.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
9, 18, 27 9, 18 27
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.