JS Quiz: Medium: Reduce accumulator initialization edge case

What happens here?

const arr = [];
const total = arr.reduce((sum, n) => sum + n);
console.log(total);
  • 0
  • undefined
  • NaN
  • TypeError
0 voters

JS Quiz answer: Option 4 (D).

Correct choice: TypeError

Why:
reduce on an empty array without an initial value throws TypeError.

Go deeper:

Arrays In JavaScript Temp

First-answer leaderboard

  1. @Apexcodes - 6 (firsts) :trophy:
  2. @kirupa - 6 (firsts) :trophy:
  3. @emmawalter5 - 5 (firsts)
  4. @adnanahmed - 2 (firsts)

Ngl I always forget this one and end up with a runtime error. it’s like the boss fight you keep thinking you’ve mastered.