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);
Actually, this is better as you can extract the reducer part (a, n) => a + n as a function. The other way is not reusable.
(a, n) => a + n
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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);
Actually, this is better as you can extract the reducer part
(a, n) => a + n
as a function. The other way is not reusable.