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
When a new copy of a datatype (object, array etc) is created instead of copying everything only the changes made to the state are copied. This saves memory.
121
+
122
+
---
123
+
<br>
124
+
125
+
## Higher Order Functions
126
+
127
+
A higher order function is a function that does one of two things. It either (1) takes one or more functions as arguments, or (2) returns a function as a result, often called a callback.
128
+
129
+
<i>Example 1:</i>
130
+
131
+
```javascript
132
+
consthof= () => () =>'hello';
133
+
134
+
hof() // -> Function () => 'hello'
135
+
136
+
hof()() // -> hello
137
+
```
138
+
139
+
<i>Example 2:</i>
140
+
141
+
```javascript
142
+
consthof= (fn) =>fn('hello');
143
+
144
+
hof(function(x){ console.log(x) }) // -> hello
145
+
```
146
+
147
+
---
148
+
<br>
149
+
150
+
## Currying
151
+
152
+
Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument.
153
+
154
+
<i>Example</i>
155
+
156
+
```javascript
157
+
constmultiply= (a,b) => a * b;
158
+
```
159
+
160
+
Instead of this function taking two parameters, we're going to give the first function one parameter which returns another function, and that function will take another parameter which will multiply `a` with `b`. Because of closures we can access the `a` variable inside of the `b` function.
161
+
162
+
```javascript
163
+
constcurriedMultiply= (a) => (b) => a * b;
164
+
165
+
curriedMultiply(5)(4); // -> 20
166
+
```
167
+
168
+
### Why is this useful?
169
+
170
+
You can now create additional utility functions ontop of this function. So you can see that in this example we're essentially saving the first function with a set argument passed into the parameter to a new variable. Then when we can use that variable as a new function which only takes an input for the second function.
0 commit comments