Get the Index of the Max/Min value in Array in JavaScript

Borislav Hadzhiev

Last updated: Dec 21, 2022

Reading timeยท5 min

**To get the index of the max value in an array:**

- Use the
`Math.max()`

method to get the max value in the array. - Use the
`String.indexOf()`

method to get the index of the max value. - The
`indexOf()`

method returns the index of the value in the array or`-1`

if the value isn't found.

index.js

`const arr = [3, 5, 8, 100, 20]; const max = Math.max(...arr); const index = arr.indexOf(max); console.log(index); // ๐๏ธ 3`

If you need to get the index of the min value, use the `Math.min()`

function
instead.

index.js

`const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // ๐๏ธ 2`

We used the Math.max() method to get the max value in the array.

The `Math.max()`

method expects multiple, comma-separated numbers as arguments,
so we can't just pass it an array directly.

index.js

`const max = Math.max(3, 5, 8) console.log(max) // ๐๏ธ 8`

We used the
spread operator (...) to
unpack the values of the array and passed them as multiple, comma-separated
arguments to the `Math.max()`

method.

index.js

`const arr = [3, 5, 8, 100, 20]; const max = Math.max(...arr); const index = arr.indexOf(max); console.log(index); // ๐๏ธ 3`

The last step is to use the
Array.indexOf
method to get the index of the `max`

value.

If we had multiple array elements with the same max value, the

`indexOf`

method would return only the index of the first occurrence.You can use a `for`

loop to get the index of all max values in the array.

**To get the indexes of all elements with the max value in an array:**

- Use the
`Math.max()`

method to get the max value in the array. - Declare an
`indexes`

variable that stores an empty array. - Iterate over the array and push only the indexes of the max values to the
`indexes`

array.

index.js

`const arr = [3, 5, 8, 100, 20, 100]; const max = Math.max(...arr); const indexes = []; for (let index = 0; index < arr.length; index++) { if (arr[index] === max) { indexes.push(index); } } console.log(indexes) // ๐๏ธ [3, 5]`

We used a for loop to iterate for `array.length`

iterations.

On each iteration, we check if the element at that index is the max value, and if it is, we push the current index to the

`indexes`

array.The `indexes`

array stores the index of all occurrences of the `max`

value in
the array.

You can also use the `Array.reduce()`

method.

This is a three-step process:

- Use the
`Array.reduce()`

method to iterate over the array. - Check if the current element is greater than the accumulator.
- If the condition is met, return the current index, otherwise, return the accumulator.

index.js

`const arr = [3, 5, 8, 100, 20]; const index = arr.reduce((accumulator, current, index) => { return current > arr[accumulator] ? index : accumulator; }, 0); console.log(index); // ๐๏ธ 3`

The function we passed to the `Array.reduce()`

method gets called with the
accumulator value, the current array element and the current index.

The second argument we passed to

`Array.reduce()`

is used as the initial value for the `accumulator`

variable.We used `0`

as the initial value.

Whatever we return from the function gets set as the new value of the accumulator variable.

On each iteration, we check if the current list item is greater than the element
at the `accumulator`

index.

If the condition is met, the current index is returned, otherwise, the
`accumulator`

index is returned.

After the last iteration, the `index`

variable stores the index of the max value
in the array.

**To get the index of the min value in an array:**

- Use the
`Math.min()`

method to get the min value in the array. - Use the
`Array.indexOf()`

method to get the index of the min value. - The
`indexOf`

method returns the index of the first occurrence of the value in the array.

index.js

`const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // ๐๏ธ 2`

We used the spread (...) operator when we called the Math.min() method.

The

`Math.min()`

method expects comma-separated numbers as arguments, so we can't directly pass it an array.index.js

`const min = Math.min(10, 5, 0) console.log(min) // ๐๏ธ 0`

We used the spread operator (...) to unpack the values of the array and passed
them as multiple, comma-separated arguments to the `Math.min()`

method.

We then used the
Array.indexOf
method to find the index of the first occurrence of the `min`

value.

index.js

`const arr = [10, 5, 0, 15, 30]; const min = Math.min(...arr); const index = arr.indexOf(min); console.log(index); // ๐๏ธ 2`

If there were multiple array elements with the value of

`0`

, the `indexOf`

method would return the index of the first occurrence.**If you need to
find the index of all occurrences
of the min value in the array, use a for loop.**

index.js

`const arr = [10, 5, 0, 15, 30, 0, 0]; const min = Math.min(...arr); console.log(min); // ๐๏ธ 0 const indexes = []; for (let i = 0; i < arr.length; i++) { if (arr[i] === min) { indexes.push(i); } } console.log(indexes); // ๐๏ธ [2, 5, 6]`

We used the `Mah.min()`

method to find the `min`

value in the array and used a
`for`

loop to iterate over the array.

On each iteration, we check if the current array element is equal to the `min`

value.

If the condition is met, we push the current index into the `indexes`

array.

The `indexes`

array stores the index of all occurrences of the `min`

value in
the array.

You can also use the `Function.apply`

method as an alternative to the spread
(...) operator.

This is a three-step process:

- Use the
`Function.apply()`

method in the call to the`Math.min()`

method. - Use the
`Array.indexOf()`

method to get the index of the min value. - The
`indexOf`

method returns the index of the first occurrence of the value in the array.

index.js

`const arr = [10, 5, 0, 15, 30]; // ๐๏ธ now using apply, instead of ... const min = Math.min.apply(null, arr); const index = arr.indexOf(min); console.log(index); // ๐๏ธ 2`

The only difference in the code is how we get the min value in the array.

The arguments we passed to the Function.apply() method are:

- The
`this`

argument - for our purposes it is irrelevant. - The array whose values will be passed to the
`Math.min()`

method as multiple, comma-separated arguments.

Under the hood, the `apply`

method unpacks the values of the array and passes
them as multiple arguments to the function the method is called on.

You can learn more about the related topics by checking out the following tutorials: