Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 15 / 34

Higher-order functions & array methods

01

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 .

Choose an array method by its result shape
MethodWhat its callback doesWhat you get back
mapReturns a replacement for each itemA new array of the same length (for a dense input)
filterReturns whether to keep each itemA new array of selected original items
reduceReturns the next accumulatorOne accumulated value; an initial value handles empty input
forEachPerforms work for each itemundefined; it does not collect callback results
findTests for the first matchThat item, or undefined
someTests whether any item matchesA boolean; false for an empty array
everyTests whether all items matchA 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.

02

Mental model

FOLLOW THE VALUE

10, 20, 30
map × 0.9
9, 18, 27

The station produces one transformed result per price.

01 / 03Trace the transformation

03

Live example

Follow the output shape: three prices become three discounted prices, then two selected prices, then one total.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Return a transformed array

Choose the method that produces one discounted number for each input price.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

9, 18, 27
9, 18
27

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which method returns one matching element or undefined?
    02Why give reduce an initial value?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson