Skip to the solution
MediumFrontend6 min read

What is a higher-order function?

122 views

The problem

Question presented to candidate: "What is a higher-order function, and can you show me one that both takes and returns a function?"

What a strong answer should cover:

  • A higher-order function is any function that does at least one of two things: accepts a function as an argument, or returns a function as its result. Either one qualifies -- it does not need to do both.
  • Built-in array methods like map, filter, reduce, sort, and forEach are all higher-order functions -- they take a callback and use it to process the array.
  • A function that returns a function -- a factory, a decorator, or a composition helper like compose/pipe -- is a higher-order function in the other direction.
  • Higher-order functions are what enables function composition: combining small, single-purpose functions into a larger pipeline without writing custom glue code for every combination.
  • Being a higher-order function is about a function's SHAPE (what it accepts or returns), not about what the function does internally -- it is a structural classification, not a specific algorithm.

Code / implementation expected: Yes -- a runnable snippet showing HOFs that take a function (map/filter/reduce) and one that returns a function (a compose helper), with real chained output.

  • higher-order

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 7 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptHOFs that take a function: map/filter/reduce (run directly)
JavaScriptA HOF that returns a function: a compose helper (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 116 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is the difference between client-side and server-side rendering?

Continue