What is a higher-order function — and why map(parseInt) breaks your numbers

Here's a puzzle. You ask an AI to turn the strings ["1", "2", "3"] into numbers. It writes one short line:
["1", "2", "3"].map(parseInt)
You expect [1, 2, 3]. You get [1, NaN, NaN].
The code didn't crash. No error. Two out of three numbers just became "not a number". It isn't parseInt's fault, or map's. It's about how one function calls another. That's what higher-order functions are — let's dig in, and the puzzle solves itself.
What a higher-order function is
A higher-order function is a function that takes another function or returns a function. That's the whole definition.
You've already met them. map, filter, forEach, setTimeout, addEventListener — all higher-order. You hand them a function, and they decide when to call it and with what.
A real-life analogy. You don't bake the cake yourself — you hand the recipe to a baker. The recipe is the function. The baker is the higher-order function. They take your recipe and decide how many times to bake and from what.
The function you hand in is called a callback. The callback is what gets passed. The higher-order function is the one receiving it.
How it works: who calls your function
The key thing to get: when you pass a function, you're not the one calling it.
You write arr.map(double). You never call double yourself. map calls it — once per element. And map passes it three arguments, not one:
- the element itself,
- its index (position),
- the whole array.
Check it yourself:
[10, 20, 30].map((x, i, arr) => console.log(x, i, arr.length))
// 10 0 3
// 20 1 3
// 30 2 3
Most functions only use the first argument and silently ignore the rest. That's why things usually work. But not always.
The answer: why map(parseInt) gives NaN
parseInt accepts a second argument — the radix (number base). parseInt("ff", 16) is 255, "ff" in hexadecimal.
Now look at what map does. It hands parseInt the element and the index. The index lands right where parseInt expects the radix:
parseInt("1", 0)— zero means "default", so we get1.parseInt("2", 1)— base 1 doesn't exist →NaN.parseInt("3", 2)— binary has no digit 3 →NaN.
Hence [1, NaN, NaN]. Even better: ["10", "10", "10"].map(parseInt) gives [10, NaN, 2]. Same string — three different answers.
Two ways to fix it:
["1", "2", "3"].map(Number) // Number takes only 1 argument
["1", "2", "3"].map((s) => parseInt(s, 10)) // you decide what gets passed
The other side: a function that returns a function
A higher-order function can also create functions. Handy when you need a lot of similar ones:
function makeDiscount(percent) {
return (price) => price * (1 - percent / 100);
}
const blackFriday = makeDiscount(30);
blackFriday(1000); // 700
makeDiscount is a factory. You set the percent once and get a ready-made function. Inside, it remembers percent thanks to scope.
Where you'll see this: React handlers like onClick={() => remove(id)}, logging wrappers, debounce. AI loves these constructs — now you'll recognize them.
How to read a chain the AI wrote
Here's a typical line of generated code:
const revenue = orders
.filter((o) => o.status === "paid")
.map((o) => o.total)
.reduce((sum, t) => sum + t, 0);
Read it top to bottom, like a conveyor belt. Each line is a separate workshop:
filter— keep only paid orders.map— take just the total from each.reduce— add it all into one number, starting from zero.
The arrows inside are instructions for each workshop. The workshops themselves are higher-order functions. If the result looks odd, drop a console.log between steps and see which workshop spoiled the data.
Why this matters to you
Half of the code AI writes for you is chains like items.filter(...).map(...). Once you get that each function in the chain calls your arrow itself, you read that code like plain text. For when to pick map and when a plain loop, see loop vs map.
A rule that saves you from NaN puzzles: don't pass a ready-made function directly unless you're sure how many arguments it takes. Wrap it in an arrow: .map((x) => fn(x)). One extra pair of parentheses — and you know exactly what goes in.
Are a callback and a higher-order function the same thing?
No, they're two sides of one deal. A callback is the function being passed. A higher-order function is the one that takes it or returns a new function.
Is this only in JavaScript?
No. Python has map, filter, sorted(key=...); Swift and Kotlin collections have map and filter. Same idea: a function can be passed around like any value.
Is it slower than a plain loop?
In practice you won't notice until you have hundreds of thousands of items. Pick whatever reads easier. For a normal app, that's almost always map and filter.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





