Use a callback for each array element.

forEach Totals

for_each_total.js
const factor = 2;
const values = [1, 2, 3];
let total = 0;
values.forEach((value) => total += value * factor);

console.log("factor=" + factor);
console.log("total=" + total);
for-each-total `forEach` calls a function once for every element. It is useful when each element should update an outside value or perform a side effect.