Course contentsShow
← JavaScript
Lesson 861 of 2,72320. Array Iteration and TransformationFree lesson

Reduce Signature: Accumulator, Current, Index, Array

Master the four parameters passed to the reduce callback and understand how each one is used during iteration.

Reduce Signature: Accumulator, Current, Index, Array

You'll learn the four parameters that reduce() passes to your callback function and what each one represents during array iteration.

Understanding the Full Callback Signature

When you use reduce(), your callback function can accept up to four parameters on each iteration:

array.reduce((accumulator, current, index, array) => {
  // Your reduction logic here
}, initialValue);

Let's break down each parameter:

1. accumulator

The running total or accumulated result. On the first call, it's either the initialValue you provided or the first array element. This carries forward the result from the previous iteration.

2. current

The current element being processed in the array. This moves through each element, one at a time.

3. index (optional)

The index position of the current element. Useful when you need to know where you are in the array.

4. array (optional)

A reference to the original array you're reducing. Handy if you need to look ahead or reference other elements.

Seeing All Four in Action

const numbers = [10, 20, 30];

numbers.reduce((acc, curr, idx, arr) => {
  console.log(`Index ${idx}: acc=${acc}, curr=${curr}, arrayLength=${arr.length}`);
  return acc + curr;
}, 0);

// Output:
// Index 0: acc=0, curr=10, arrayLength=3
// Index 1: acc=10, curr=20, arrayLength=3
// Index 2: acc=30, curr=30, arrayLength=3

Most of the time, you'll only need accumulator and current. The index and array parameters are there when you need extra context during your reduction.

Key Takeaway: The reduce() callback receives four parameters—accumulator (running result), current (element being processed), index (position), and array (original array)—giving you full context during iteration.