Skip to content

Wrong Reduce Initial Value

Quick summary

Your reduce starts from the wrong seed — the accumulator's first value is off, so every subsequent step compounds the error.

Why this happens

  • Omitting the initial value so the first element becomes the accumulator

  • Using 1 instead of 0 for a sum, or 0 instead of 1 for a product

  • Passing an empty object when the reducer expects an array (or vice versa)

Minimal example

✗ Broken code
const nums = [1, 2, 3];
const product = nums.reduce((a, b) => a * b, 0); // 0 — wrong seed!
console.log(product); // 0
✓ Fixed code
const nums = [1, 2, 3];
const product = nums.reduce((a, b) => a * b, 1); // 1 is the multiplicative identity
console.log(product); // 6

Starting the product accumulator at 0 makes every multiplication yield 0. Using 1 (the multiplicative identity) gives the correct product of 6.

How to diagnose

  • Log the accumulator at each step to see the starting value

  • Test reduce on an empty array — the result reveals the seed

  • Compare the output with a hand-computed example

How to fix

  • Pass the correct identity element (0 for sum, 1 for product, [] for concat)

  • Always provide an explicit initial value rather than relying on the first element

  • Match the seed type to the accumulator type the reducer expects

How to prevent

  • Always pass an explicit initial value to reduce

  • Write a quick test for reduce on empty and single-element arrays

Related Resources

Related Glossary

Related Lessons

  • Arrays

    Learn about reduce and accumulators.

← Back to language