Higher-Order Functions
map, filter, reduce, forEach — vocabulary for describing callbacks and functional patterns
HOF vocabulary
- filter: "filters [array] and returns a new array where [condition]" — does NOT mutate
- map: "transforms each [element] into [result], returning a new array of [type]"
- reduce: "reduces to a single [type] by [accumulation logic], starting from [initial value]"
- forEach: "invoked for each element; the callback receives [element] and [index]"
- async: "asynchronously [verb]; awaits the result of; resolves with"
Question 0 of 5
Read this code: const activeUsers = users.filter(u => u.isActive);
Which description is correct?
"Filters the array and returns a new array containing only users where isActive is true" is correct. Filter vocabulary:
- "filters" — selects a subset based on a condition
- "returns a new array" — filter does NOT mutate the original
- "where [condition] is true" — describes the predicate
.filter() returns a new array (pure); .splice() mutates the original (side effect). In code review: "this correctly returns a new array rather than mutating the original — good."Describe what this code does: const prices = items.map(item => item.price * 1.2);
"Transforms each item into its price multiplied by 1.2, returning a new array of numbers" is correct. Map vocabulary:
- "transforms / maps each element to" — key verb for
.map() - "returning a new array" — map never mutates the original
- "of numbers" — name the result type (item objects become numbers)
Item[], output is number[]. Always mention the type transformation in map descriptions — "maps each Item to a number" is more informative than just "maps over the array."What does this reduce call do? const total = orders.reduce((sum, order) => sum + order.amount, 0);
"Reduces the array to a single number by summing all order amounts, starting from 0" is correct. Reduce vocabulary:
- "reduces to a single value" — the key characteristic of reduce/fold
- "accumulates by" — describes the accumulation logic
- "starting from [initial value]" — the second argument to reduce (0 here)
Describe this async function: async function loadUser(id) { const data = await fetchUser(id); return formatUser(data); }
"Asynchronously fetches a user by ID, awaits the result, then transforms it into a formatted User" is correct. Async/await description vocabulary:
- "asynchronously" — signals the async nature
- "awaits the result of" — describes the await keyword
- "then transforms / then returns" — the sequential steps after awaiting
- "resolves with" — what the returned Promise ultimately delivers
Promise.all()). This is an important distinction in code review.What does the callback in this code receive? items.forEach((item, index) => { console.log(index, item.name); });
"The callback receives the item and its zero-based index" is correct. Callback vocabulary:
- "invoked for each element" — describes the iteration
- "the callback receives [arg1] and [arg2] as arguments" — names the callback parameters
- "zero-based index" — clarifies that indices start at 0
(element, index, array) => void. All three arguments are optional — the callback can ignore any of them. In code review: "since you only need the item here, the index parameter adds noise — consider omitting it."