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
Copy file name to clipboardExpand all lines: README.md
+35-7Lines changed: 35 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -184,7 +184,7 @@ console.log(a % b); // prints 10 to the console
184
184
There is also a shorthand notation of the operations, which is used to perform operations on the same variable.
185
185
186
186
```javascript
187
-
var a =10;
187
+
let a =10;
188
188
a +=10; // a = a + 10
189
189
console.log(a); // prints 20 to the console
190
190
// Similarly, there are other operations like -=, *=, /=, %=
@@ -226,13 +226,13 @@ arr[2] = 10;
226
226
console.log(arr); // prints [undefined, undefined, 10] to the console
227
227
```
228
228
229
-
If a empty array is defined, then the array is filled with `undefined` values.
229
+
If an empty array is defined, then the array is filled with `undefined` values.
230
230
231
231
This datatype also has some special properties, we can also apply stack and queue operations on it.
232
232
233
233
**Stack Operations**
234
234
```javascript
235
-
var arr = [1, 2, 3, 4, 5];
235
+
let arr = [1, 2, 3, 4, 5];
236
236
arr.push(6); // push operation
237
237
console.log(arr); // prints [1, 2, 3, 4, 5, 6] to the console
238
238
arr.pop(); // pop operation
@@ -244,30 +244,58 @@ When running a push operation, it will return the length of the array.
244
244
245
245
**Queue Operations**
246
246
```javascript
247
-
var arr = [1, 2, 3, 4, 5];
247
+
let arr = [1, 2, 3, 4, 5];
248
248
console.log(arr.shift()); // dequeue operation (prints 1 to the console)
249
249
arr.unshift(6); // enqueue operation(to the first index)
250
250
console.log(arr); // prints [6, 2, 3, 4, 5] to the console
251
251
```
252
252
253
253
**Slicing an Array**
254
254
```javascript
255
-
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
256
-
var slice =arr.slice(2, 5); // slice(start_index, count)
255
+
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
256
+
let slice =arr.slice(2, 5); // slice(start_index, count)
257
257
console.log(slice); // prints [3, 4, 5, 6, 7] to the console
258
258
console.log(arr); // prints [1, 2, 8, 9]
259
259
```
260
260
261
261
To apply a specific operation on each element of the array, we can use the `forEach()` method.
262
262
263
263
```javascript
264
-
var arr = [1, 2, 3, 4, 5];
264
+
let arr = [1, 2, 3, 4, 5];
265
265
266
266
arr.forEach((element) => {
267
267
console.log(element);
268
268
});
269
269
```
270
270
271
+
Filtering on the array can be done, using the `filter()` method. It will return a new array, which contains the elements which satisfy the condition.
272
+
273
+
```javascript
274
+
let arr = [11, 25, 13, 42, 54, 6, 78];
275
+
276
+
let filtered =arr.filter((element) => {
277
+
return element >25;
278
+
});
279
+
console.log(filtered); // prints [42, 54, 78] to the console
280
+
// Here you can see that only elements greater than 25 are returned
281
+
```
282
+
283
+
The next method, which we are going to learn is the find method. It will return the first element which satisfies the condition. If no element is matched then, it will return `undefined`.
284
+
285
+
```javascript
286
+
let arr = [11, 25, 13, 42, 54, 6, 78];
287
+
288
+
let filtered =arr.find((element) => {
289
+
return element >25;
290
+
});
291
+
console.log(filtered); // prints 42 to the console
292
+
```
293
+
294
+
The main, difference between the `filter()` and `find`() methods is the return datatype of the methods, `filter()` returns an array, whereas `find()` returns the element.
295
+
296
+
* Filter will always return an array, even if there is only one element or no element in the array.
297
+
* Find will return the element, if there is only one element in the array, otherwise it will return undefined.
298
+
271
299
## Conditions
272
300
273
301
Conditions are used to check whether a condition is true or false, and based on that we can perform some operations.
0 commit comments