1const array1 = ['a', 'b', 'c'];
2
3array1.forEach(element => console.log(element));
1someValues.forEach((element, index) => {
2 console.log(`Current index: ${index}`);
3 console.log(element);
4});
1// Arrow function
2forEach((element) => { ... } )
3forEach((element, index) => { ... } )
4forEach((element, index, array) => { ... } )
5
6// Callback function
7forEach(callbackFn)
8forEach(callbackFn, thisArg)
9
10// Inline callback function
11forEach(function callbackFn(element) { ... })
12forEach(function callbackFn(element, index) { ... })
13forEach(function callbackFn(element, index, array){ ... })
14forEach(function callbackFn(element, index, array) { ... }, thisArg)
15
1const products = [
2 { name: 'laptop', price: 32000, brand: 'Lenovo', color: 'Silver' },
3 { name: 'phone', price: 700, brand: 'Iphone', color: 'golden' },
4 { name: 'watch', price: 3000, brand: 'Casio', color: 'Yellow' },
5 { name: 'sunglass', price: 300, brand: 'Ribon', color: 'blue' },
6 { name: 'camera', price: 9000, brand: 'Lenovo', color: 'gray' },
7];
8//Total Products Price by Using forEach
9let totalPrice = 0;
10products.forEach(product => {
11 totalPrice += product.price;
12})
13console.log(totalPrice);
14//Expected output: 45000