JavaScript Notes
Master JavaScript higher-order functions – functions that take or return functions. Learn map, filter, reduce, compose, curry, and build custom HOFs with real-world examples.
Introduction
Higher-order functions (HOFs) are one of the most powerful features in JavaScript — and they are what make JavaScript a functional programming language.
They allow you to:
- Write more readable and reusable code
- Build abstractions that separate "what to do" from "how to do it"
- Power built-in methods like
map,filter, andreduce
If you've ever used .map() or .filter() on an array, you've already used higher-order functions!
Why Higher-Order Functions?
[2, 4, 6] [1, 4, 9]
One function does the work of many — just pass different logic as an argument.
Real World Analogy
Higher-order functions are like a factory machine with interchangeable toolheads. The machine (HOF) does the mechanical work (loop, iterate, manage state). You plug in a different tool (function) to produce different output. 🏭 Machine + 🔪 Blade tool = Sliced products 🏭 Machine + 🔥 Heat tool = Heated products 🏭 Machine + 🎨 Painting tool = Painted products Same machine. Different behavior. Controlled by the tool you pass in.
Built-in Higher-Order Functions
1. Array.prototype.map() – Transform Every Element
map creates a new array by applying a function to each element.
[90, 225, 72, 387]
2. Array.prototype.filter() – Keep Matching Elements
filter creates a new array with only elements that pass a test.
[22, 30, 18, 25]
3. Array.prototype.reduce() – Reduce to a Single Value
reduce accumulates array elements into one result.
Total: 1130
4. Array.prototype.forEach() – Execute for Each (no return value)
1. Riya 2. Arjun 3. Priya
5. Array.prototype.find() – Find First Match
{ id: 2, name: 'Arjun' }6. Array.prototype.sort() – Custom Sorting
Arjun: 95 Riya: 88 Priya: 76
Functions That Return Functions (HOF Pattern 2)
Higher-order functions can also return a new function.
function multiplier(factor) {
// returns a function — this is a HOF
return function (number) {
return number * factor;
};
}
const double = multiplier(2);
const triple = multiplier(3);
const tenX = multiplier(10);
console.log(double(5));
console.log(triple(5));
console.log(tenX(5));10 15 50
Building Custom Higher-Order Functions
Custom map Implementation:
[1, 4, 9, 16]
Custom filter Implementation:
[2, 4, 6]
Custom compose (Function Composition):
400
Step-by-Step Trace: Chaining HOFs
['Priya', 'Riya']
Common Mistakes
❌ Mistake 1: Mutating the original array inside map
[1, 2, 3, 10, 20, 30] ← unexpected mutation!
[2, 4, 6] [1, 2, 3]
❌ Mistake 2: Using forEach when you need the result
[2, 4, 6]
❌ Mistake 3: Forgetting reduce's initial value
6
❌ Mistake 4: Confusing filter and find
[2, 2] 2
HOFs vs Imperative Loops – Comparison
[1, 9, 25] [1, 9, 25]
Same result — but the HOF version is shorter, more readable, and expresses what to do rather than how.
Interview Questions
Q1. What is a higher-order function?
A higher-order function is a function that either takes a function as an argument, returns a function, or both.
Q2. What are the most common built-in higher-order functions in JavaScript?
map, filter, reduce, forEach, find, findIndex, sort, every, some, flatMap.
Q3. What is the difference between map and forEach?
map returns a new transformed array. forEach returns undefined and is used for side effects only.
Q4. What is the difference between filter and find?
filter returns an array of all matching elements. find returns the first matching element (or undefined).
Q5. How does reduce work?
reduce(callback, initialValue) applies the callback to an accumulator and each element, returning one final value. The callback takes (accumulator, currentValue).
Q6. What makes JavaScript functions "first-class"?
In JavaScript, functions are first-class values — they can be stored in variables, passed as arguments, returned from functions, and stored in arrays/objects. This is what enables higher-order functions.
Q7. How do you build a custom higher-order function?
Accept a function as a parameter and call it at the right time:
Step 1 Step 2 Step 3
Q8. What is function composition and how do HOFs enable it?
Function composition chains functions together so the output of one becomes the input of the next. HOFs like compose and pipe implement this pattern by returning a new function that applies each step in sequence.
Key Takeaways
- ✅ A HOF either takes or returns a function (or both)
- ✅
maptransforms each element into a new array (non-mutating) - ✅
filterkeeps only elements that pass a test - ✅
reduceaccumulates elements into a single value - ✅
forEachis for side effects only — returnsundefined - ✅ Always provide an initial value to
reduce - ✅ HOFs make code declarative — describe what to do, not how
- ✅ Functions are first-class values in JS — HOFs are possible because of this
- ✅ You can build custom HOFs by accepting functions as parameters
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Higher Order Functions Tutorial – map, filter, reduce & Custom HOFs.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, functions, higher, order
Related JavaScript Master Course Topics