ProgramGuru

Higher-Order Functions

Understand Higher-Order Functions

A higher-order function accepts a function, returns a function, or does both. It helps separate a reusable operation from the specific behavior being applied.

What you will learn: the purpose of higher-order functions, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how higher-order functions works and where its type rules apply.
  • Predict the exact output of the higher-order functions example.
  • Diagnose a common issue: Do not confuse returning a function with returning the result of calling that function.

Build the mental model

TypeScript can express functions as input or output values. In this example makeMultiplier captures factor and returns another typed function, so each returned function has its own remembered factor.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Higher-Order Functions: explained visuallymakeMultiplier(2) → Function value → twice(5) → 10makeMultiplier(2)Function valuetwice(5) → 10Follow the values through the operation
Higher-Order Functions: makeMultiplier(2) → Function value → twice(5) → 10.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

function makeMultiplier(factor: number): (value: number) => number {
  return (value: number) => factor * value;
}
const twice = makeMultiplier(2);
const thrice = makeMultiplier(3);
console.log(twice(5));
console.log(thrice(5));

Verified output

10
15

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

makeMultiplier(2) returns a function remembering 2. makeMultiplier(3) returns a separate function remembering 3. Calling each with 5 yields 10 and 15.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not confuse returning a function with returning the result of calling that function. The return type here is itself a function signature.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Create makeAdder(offset) and use it to build addTen. Explain what survives after makeAdder returns.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Why is makeMultiplier higher-order?
Choose one answer. You can try again.

Answer and explanation

It returns another function.

A higher-order function accepts a function, returns a function, or does both. It helps separate a reusable operation from the specific behavior being applied.

Review the key idea

Higher-Order Functions: A higher-order function accepts a function, returns a function, or does both.

makeMultiplier(2) returns a function remembering 2. makeMultiplier(3) returns a separate function remembering 3. Calling each with 5 yields 10 and 15.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Closures →