Lesson 7 of 80
Functions that take or return functions — the idea that map, filter, and reduce all share.
13 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
What makes a function a 'higher-order function'?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A higher-order function is just a function that takes another function as an argument, or returns one. That's the whole definition. It works because in JavaScript functions are values — you can pass them around like numbers or strings.
map, filter, and reduce are all higher-order functions: you hand them a small function describing one step, and they handle the looping.
const nums = [1, 2, 3, 4];
const doubled = nums.map((n) => n * 2); // transform each -> [2, 4, 6, 8]
const evens = nums.filter((n) => n % 2 === 0); // keep some -> [2, 4]
You wrote the step (n to n * 2); map ran it across the list and built a new array. Same shape as the reduce lesson: describe one step, let the tool repeat it.
A higher-order function takes a function as an argument, returns one, or both.
map returns a new array; it never mutates the original. That immutability is exactly why React leans on it so heavily.map must return a value. Forgetting the return, or using it where forEach was meant, gives an array full of undefined.These share one root — the higher-order function — and lean on closures, since the callback often closes over surrounding variables. Master this and the entire array-method family stops feeling like memorization.