You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, `processUserInput` is a higher order function because it takes another function (`callback`) as an argument. You pass the `greet` function to it, so `processUserInput` calls `greet` with the name "Aditya". This is a common pattern for handling user input, events, or callbacks.
44
+
45
+
---
46
+
47
+
### 2. Returning a Function (Closure)
39
48
40
-
### 2. Returning a function
41
49
```js
42
50
functionmultiplier(factor) {
43
51
returnfunction(number) {
44
52
return number * factor;
45
53
};
46
54
}
55
+
47
56
constdouble=multiplier(2);
48
57
console.log(double(5)); // Output: 10
49
58
```
59
+
**Explanation:**
60
+
`multiplier` is a higher order function because it returns a new function. When you call `multiplier(2)`, you get a function that multiplies any number by 2. This is called a closure, because the returned function "remembers" the value of `factor`. So, `double(5)` gives you `10`.
61
+
62
+
---
50
63
51
64
### 3. Built-in Higher Order Functions
52
65
There are various built in HOFs, and some of the most common ones are map(), filter() and reduce().
@@ -75,6 +88,8 @@ function repeat(n, action) {
75
88
// Here, console.log is passed as the action, so it logs 0, 1, 2
76
89
repeat(3, console.log); // Logs 0, 1, 2
77
90
```
91
+
**Explanation:**
92
+
The `repeat` function is a higher order function because it takes another function (`action`) as an argument. You can pass any function to `repeat`, and it will call that function for each number from 0 to n-1. In this example, we pass `console.log`, so it prints 0, 1, and 2.
78
93
79
94
### 5. Returning Functions (Closures)
80
95
```js
@@ -92,6 +107,8 @@ console.log(counter()); // 1
92
107
console.log(counter()); // 2
93
108
// Each call to counter() increases and returns the count
94
109
```
110
+
**Explanation:**
111
+
`makeCounter` is a higher order function because it returns a new function. The returned function "remembers" the value of `count` even after `makeCounter` has finished running. This is called a closure. Each time you call `counter()`, it increases and returns the count, keeping track of how many times it was called.
// add1ThenTimes2 first adds 1 to 5, then multiplies the result by 2
109
126
```
127
+
**Explanation:**
128
+
The `compose` function is a higher order function because it takes two functions as arguments and returns a new function. The returned function applies `g` to its input, then applies `f` to the result. In the example, `add1ThenTimes2(5)` first adds 1 to 5 (getting 6), then multiplies by 2 (getting 12).
110
129
111
130
## Interview Approach Example: Calculating Area and Circumference
112
131
@@ -131,6 +150,8 @@ const calculateCircumference = function (radius) {
131
150
};
132
151
console.log(calculateCircumference(radius));
133
152
```
153
+
**Explanation:**
154
+
This approach works, but it repeats similar logic for area and circumference. If you need to add more calculations, you would have to write more similar functions, which is not efficient and violates the DRY (Don't Repeat Yourself) principle.
Here, we use a higher order function `calculate` that takes an array and a function (`operation`) as arguments. This makes the code reusable and clean. You can pass any calculation logic (like area or circumference) to `calculate`, and it will apply that logic to each element in the array. This is the power of higher order functions and functional programming.
This is a custom implementation (polyfill) of the built-in `map` function. By adding `calculate` to `Array.prototype`, you can use it on any array just like `map`. It takes a function as an argument and applies it to every element in the array, returning a new array with the results.
183
208
184
209
## Summary
185
210
Higher order functions are a key concept in JavaScript, enabling powerful patterns for abstraction, reusability, and functional programming.
0 commit comments