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

Map's Callback Parameters

Exploring the three parameters passed to map callbacks: element, index, and array.

Map's Callback Parameters

What you'll learn: The three pieces of information JavaScript passes to your map callback function on each iteration.

When you use map(), you're not limited to just working with the array element itself. JavaScript automatically provides your callback function with three parameters every time it's called:

  1. element – The current item being processed
  2. index – The position of that item in the array (starting at 0)
  3. array – The entire original array being mapped

Think of it like a conveyor belt where each item comes with a label showing its position and a reference manual showing all items on the belt.

The Three Parameters in Action

const numbers = [10, 20, 30];

const result = numbers.map((element, index, array) => {
  console.log(`Element: ${element}`);
  console.log(`Index: ${index}`);
  console.log(`Array: ${array}`);
  return element * 2;
});

// Logs:
// Element: 10, Index: 0, Array: [10, 20, 30]
// Element: 20, Index: 1, Array: [10, 20, 30]
// Element: 30, Index: 2, Array: [10, 20, 30]

You Don't Need All Three

Most of the time, you only need the element:

const doubled = numbers.map(element => element * 2);

But the index is handy when position matters:

const withPosition = numbers.map((element, index) => {
  return `Item ${index + 1}: ${element}`;
});
// ['Item 1: 10', 'Item 2: 20', 'Item 3: 30']

The array parameter is rarely used but available if you need to reference the original array during transformation.

Key Takeaway: Every map callback receives three parameters—element, index, and array—but you only need to declare the ones you'll actually use.