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
The filter function works on arrays and it filters out elements in our array.
68
70
It takes a function as input. This function is called on every element in the array. Just like forEach. If this function we create returns true the element is saved in the, if we return false the element is filtered out.
69
71
70
-
71
72
```js
72
73
73
74
// Lets get only cars with a speed larger than 60
@@ -86,9 +87,6 @@ console.log(fastCars); // logs the BMW and the Fiat
86
87
```
87
88
88
89
89
-
90
-
91
-
92
90
### Sort
93
91
94
92
The sort function works on arrays. It sorts the elements of the array.
@@ -105,6 +103,35 @@ const sortedCars = cars.sort(function(a, b) {
105
103
console.log(sortedCars); // it will return an array with the BMW object first, then the fiat and then the volvo
106
104
```
107
105
106
+
### Arrow functions
107
+
Functions can be written as arrow functions, it looks like this:
108
+
109
+
```js
110
+
// This function
111
+
functiongetTimesTen(a) {
112
+
return a *10;
113
+
}
114
+
115
+
// Can be written like this:
116
+
constgetTimesTenArrowFunction= (a) => {
117
+
return a *10;
118
+
}
119
+
120
+
// If there is only one parameter, we can remove the paranthesis:
121
+
constgetTimesTenArrowFunction=a=> {
122
+
return a *10;
123
+
}
124
+
125
+
// If the function is returning a single line of code, you can shorten it even further:
126
+
constgetTimesTenArrowFunction=a=> a *10;
127
+
```
128
+
129
+
Here is how you **convert a function into an arrow function:**
130
+
1. Remove the keyword function
131
+
2. Add an arrow after the parameter
132
+
3. If there is only one parameter, we can remove the paranthesis around the parameter
133
+
4. If the function is returning a single line of code, we can remove the return keyword and the curly braces.
| 5. | Functions advanced <br> Asynchronicity in js |[Preparation](/JavaScript2/Week5/preparation.md)|[Homework](/JavaScript2/Week5/homework.md)|
7
-
| 6. | Array function <br> Higher order functions <br> Functional composition|[Preparation](/JavaScript2/Week6/preparation.md)|[Homework](/JavaScript2/Week6/homework.md)|
7
+
| 6. | Array function <br> Arrow function <br> Higher order functions <br> Functional composition|[Preparation](/JavaScript2/Week6/preparation.md)|[Homework](/JavaScript2/Week6/homework.md)|
0 commit comments