-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37-forEach.js
More file actions
44 lines (28 loc) · 921 Bytes
/
Copy path37-forEach.js
File metadata and controls
44 lines (28 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// forEach loop works with callback means takes function as parameter and provides the argument one by one.
/* first way
const numbers = [2, 4, 5, 6];
function multiplyby2(number, index) {
console.log("index is", index);
console.log(number * 2);
}
numbers.forEach(multiplyby2);
*/
/* Second way - creating anonymous function inside forEach
const numbers = [2, 4, 5, 6];
numbers.forEach(function (number) {
console.log(`${number} * 2 is ${number * 2}`);
});
*/
/* Third way - using arrow functions
const numbers = [2, 4, 6, 7];
numbers.forEach((number) => {
console.log(number * 2);
});
*/
/* Fourth way - working with array objects */
const users = [
{ firstName: "Vaishnao", gender: "Male" },
{ firstName: "Vaishnavi", gender: "Female" },
{ firstName: "Arpit", gender: "Male" },
];
users.forEach((user) => console.log(user.firstName)); // the use of simplest arrow function is used here.