This code works:
const sum = (x, y, z) => {
const args = [x, y, z];
return args.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3)); // 6
This code does the same thing:
const sum = (x, y, z) => {
const args = [x, y, z];
return args.reduce((a, b) => a + b);
}
console.log(sum(1, 2, 3)); // 6
The difference is that I removed “, 0” in the second arrow function.
Why was the ‘, 0’ included in the original code?
I feel like there must be something that I’m missing in this. Is the 0 a default value?