The reduce() method executes a reducer function on each element of the array.

arr.reduce(callback( accumulator, currentValue, [, index[, array]] )[, initialValue])
const numbers = [3, 5, 8, 9, 12];const total = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);console.log(total);
// 37
const numbers = [3, 5, 8, 9, 12];
const result = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 3);
console.log(result);
// 40
const numbers = [3, 5, 8, 9, 12];
const result = numbers.reduce((accumulator, currentValue) => accumulator - currentValue, 40);
console.log(result);
// 3
const numbers = [3, 5, 8, 9, 12];
const result = numbers.reduce((accumulator, currentValue) => {
console.log(accumulator, currentValue);
return accumulator + currentValue;
}, 0);
console.log('Result: ', result);
// 0 3
// 3 5
// 8 8
// 16 9
// 25 12
// 25 12
const employeeList = [
{
name: 'Piter',
salary: 1200
},
{
name: 'Harry',
salary: 1500
},
{
name: 'Mark',
salary: 1800
}
];
const totalSalary = employeeList.reduce((sum, employee) => sum + employee.salary, 0);console.log(totalSalary);
// 4500

in a relationship with console.log()