'Why is reduce index 1-based sometimes? [duplicate]

I've just come across an annoying bug (in my code) that was caused by the JavaScript Array.reduce method.

I've created a minimal reproduction of the issue below.

const people = [{name: 'bob'}, {name: 'fred'}, {name: 'john'}]

people.reduce((acc, person, i) => {
  if (i === 0) throw Error('this error is never thrown')
  if (i === 1) console.log(acc === people[0])
  console.log(i, person.name)
  return acc
})

people.reduce((acc, person, i) => {
  if (i === 0) console.log(acc === null)
  console.log(i, person.name)
  return acc
}, null)

Why does the first piece of code above iterate twice only whilst the second use of reduce iterates three times (once for each array item) as expected?

Are there any docs documenting this behaviour or is this a bug in Chrome? If its not a bug, why does this behaviour exist? TIA



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source