Skip to main content

How to Get the Index of the Max or Min Value in an Array in JavaScript

A common task in data analysis is to find not just the maximum or minimum value in an array of numbers, but also its index (or position). This is useful for identifying the data point that corresponds to a peak or a low point.

This guide will teach you the modern, standard methods for finding the index of the max or min value. You will learn the simple two-step approach for finding the first occurrence and a looping method for finding all occurrences if the extreme value appears multiple times.

The Core Logic: Find the Value, then Find the Index

The most intuitive approach is a two-step process:

  1. Find the Extreme Value: First, determine the maximum or minimum value in the array. The Math.max() and Math.min() methods are perfect for this, especially when combined with the spread syntax (...).
  2. Find the Index: Once you have the value, use the Array.prototype.indexOf() method to find the index of its first occurrence in the array.

Goal 1: Get the Index of the First Max or Min Value

This is the most common requirement. The two-step process described above is the most readable and direct way to solve it.

For example, we have an array of numbers and need to find the index of the highest (or lowest) value.

// Problem: What is the index of the max/min value in this array?
const numbers = [10, 50, 30, 100, 40];

Recommended Solution:

const numbers = [10, 50, 30, 100, 40, 100];

// --- Get the Index of the MAX Value ---
const maxValue = Math.max(...numbers); // Finds the max value: 100
const maxIndex = numbers.indexOf(maxValue);

console.log('Max value:', maxValue); // Output: Max value: 100
console.log('Index of max:', maxIndex); // Output: Index of max: 3

// --- Get the Index of the MIN Value ---
const minValue = Math.min(...numbers); // Finds the min value: 10
const minIndex = numbers.indexOf(minValue);

console.log('Min value:', minValue); // Output: Min value: 10
console.log('Index of min:', minIndex); // Output: Index of min: 0

Output:

Max value: 100
Index of max: 3
Min value: 10
Index of min: 0
note

Important: indexOf() returns the index of the first occurrence only. In the example above, even though 100 appears twice, indexOf() only returns 3.

An Alternative for Goal 1: Using reduce()

For a more functional, single-pass solution, you can use the Array.prototype.reduce() method. This can be more performant on extremely large arrays as it avoids a second pass over the array.

Logic: we iterate through the array, keeping track of the index of the largest value seen so far.

Solution:

const numbers = [10, 50, 30, 100, 40];

const maxIndex = numbers.reduce((bestIndex, currentValue, currentIndex, array) => {
// If the current value is greater than the value at our best index,
// the current index becomes the new best index.
return currentValue > array[bestIndex] ? currentIndex : bestIndex;
}, 0); // Start with the assumption that the first element is the largest.

console.log('Index of max (using reduce):', maxIndex); // Output: 3
note

While clever and efficient, this solution can be less intuitive for developers who are not as comfortable with reduce().

Goal 2: Get the Indexes of All Max or Min Values

If the maximum or minimum value can appear multiple times, you might need to find all of its indexes.

For example, we want to find all indexes where the value is 100.

// Problem: How to find all indexes of the max value?
const numbers = [10, 100, 30, 100, 40];
// Expected result: [1, 3]

This requires a loop or a functional chain. A for loop is often the clearest approach.

  1. First, find the max/min value.
  2. Create an empty array to store the result indexes.
  3. Loop through the original array and push the index of any matching element into your results array.
const numbers = [10, 100, 30, 100, 40];
const maxValue = Math.max(...numbers);
const allMaxIndexes = [];

for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === maxValue) {
allMaxIndexes.push(i);
}
}

console.log('All indexes of max value:', allMaxIndexes); // Output: [1, 3]

Conclusion

Finding the index of the maximum or minimum value in an array is a straightforward task with a few clear solutions.

  • To find the first index of a max/min value, the most readable method is the two-step approach: Math.max(...arr) followed by arr.indexOf().
  • The reduce() method provides a more performant, single-pass alternative for finding the first index.
  • To find all indexes of a max/min value, you must first find the value and then loop through the array to collect all matching indexes.