I've been learning JavaScript for a while and I understand how to write and call functions, but I still get confused by the concepts of first-class functions, higher-order functions, and closures. What do these terms mean, and what are some simple examples or exercises that could help me understand how they relate to one another?
4 Answers
One small correction to a common explanation: a normal function that only accepts numbers is not generally called a “first-order function” in everyday JavaScript terminology. The important term to learn is first-class function, meaning functions are values that can be handled like other data. Also, closures do not automatically capture every variable in existence; an inner function retains access to the relevant outer lexical environments, and those environments remain available while the function needs them.
A first-class function is simply a function that JavaScript lets you treat like any other value. You can store it in a variable, pass it to another function, or return it from a function. A higher-order function is a function that accepts another function as an argument, returns a function, or does both. For example, `array.map(x => x * x)` uses a callback function, so `map` is a higher-order function. A closure happens when an inner function remembers variables from the scope where it was created, even after the outer function has finished. Try writing small examples and experimenting with callbacks and returned functions.
Here’s a compact example showing all three ideas: `const makeAdder = start => value => start + value;`. The outer function returns another function, so `makeAdder` is a higher-order function. The returned function is a first-class value because it can be assigned to a variable: `const addFive = makeAdder(5);`. When you call `addFive(10)`, it returns `15`. The inner function still has access to `start`, even though `makeAdder` has already finished running. That remembered access is the closure.
Think of the terms as descriptions of different things. First-class functions describe what functions can do: they can be stored and passed around as values. Higher-order functions describe a function’s behavior: it receives functions or produces them. A closure describes how a function retains access to variables from an outer scope. A useful exercise is to build a function that accepts a callback, then build another function that returns a callback. Log the inputs and outputs, and change the variable values to see which ones the inner function remembers. Documentation and hands-on experiments are more useful here than trying to memorize the definitions.

Thanks! The example with `makeAdder` makes the connection between the three concepts much clearer. I’ll experiment with similar functions and use the debugger to inspect the values.