-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path43ForEach.js
34 lines (28 loc) · 900 Bytes
/
43ForEach.js
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
/* For Each Method -- its an array method similar to for of loop used iterate array over the elements*/
// let arr = [1,2,3,4,5];
// let print = function(el){
// console.log(el)
// };
// arr.forEach(print)
let arr = [
{
name: "abc",
marks: 99,
},
{
name: "xyz",
marks: 100,
},
{
name: "pqr",
marks: 89,
},
];
// arr.forEach((stu) => {
// //here stu is indiviual objects
// console.log(stu.marks);
// });
for (let stu of arr) {
console.log(stu.marks);
}
/*In a for...of loop, you can use the break statement to exit the loop prematurely, while in the forEach method, you cannot directly break out of the loop. The forEach method will iterate over all elements of the array unless an exception is thrown. for...of loop doesn't return any value by default. forEach method always returns undefined, regardless of what the callback function returns. */