reduce()
.reduce() reduces an array into one final value.
Use it when you want a total, maximum, minimum, grouped object, or combined result.
Total Example
const prices = [5, 30, 10, 25, 15, 20]
const total = prices.reduce((previous, next) => {
return previous + next
}, 0)
console.log(`$${total.toFixed(2)}`)Maximum Example
const grades = [56, 38, 90, 25, 15, 70]
const maximum = grades.reduce((max, grade) => Math.max(max, grade))
console.log(maximum) // 90