DEV Community

Discussion on: You don't need Array.reduce()

Collapse
 
nicowernli profile image
Nicolás Wernli

First example could be simpler with reduce, I mean, if you use n as variable in the for version, why not using n in the reduce one?

const array = [1, 2, 3, 4];
const sum = array.reduce((a, n) => a + n, 0);

Collapse
 
theodesp profile image
Theofanis Despoudis

Actually, this is better as you can extract the reducer part
(a, n) => a + n as a function. The other way is not reusable.