Get Index of Max Value in Array in JavaScript

Using indexOf() with Math.max() Method

To get an index of the max value in a JavaScript array:

  • Use the Math.max() function to find the maximum number from the given numbers. Here, we passed an array and used the spread operator (...) to expand the array into individual elements.
  • Use the .indexOf() function to get the index of the maximum value that we received from the previous step.

We used the Math.max() for this example to get the maximum number from the given numbers as input. Here, we gave an array as an input and used the spread operator (...) to expand the array into individual elements, which means [5,6,7,2,8,0] will turn into 5,6,7,2,8,0.

Math.max() returns NaN if any of the values from the specified numbers would be NaN or converted into NaN. The Math.max() returns -infinity if no parameter is given.

Next, we used the .indexOf() to get the index of the maximum value that we got from the Math.max() function. Always keep the following points in mind while using the .indexOf() function with an array in JavaScript:

  • It returns the first index/position of the given value. So, for instance, if we had two 8 in our array, then the maximum number would be 8, but .indexOf() would return the index of the first maximum value, which is the index of the first 8 in the array.
  • It returns -1 if no value is found.
  • By default, it starts searching from the first element and continues till the end, but we can start it from a user-defined position by specifying the start value. This way, it will start searching from the start point and move from left to right.
  • For the negative start value, it counts from the last element but still searches from left to right.

If you don’t want to use the spread operator (...) to pass the array to the Math.max() method, then we can use Math.max.apply() as follows. Remember, we can not pass the array directly to Math.max().

Here, we passed the my_array array to the apply() method directly, which applied the array as parameters to an actual function, Math.max() here. You might have a question about how .apply() worked here.

Here, the Math.max.apply(Math,my_array) can be understood as Math.max(5,6,7,2,8,0). This is how the .apply() method applied the array as parameters to an actual function.

Using for loop

To get an index of the maximum value in a JavaScript array:

  • Initialize maximum value with first element of the array.
  • Iterate over the array using for loop.
  • In each iteration, compare if current element is greater than maximum value.
  • If yes, update the maximum value and its index.
  • Once loop is finished, we will get index of maximum value in the array.

First, we wrote a function named indexOfMaximumValue(), which took an array called my_array as a parameter. Inside this function, we used the if statement with strict equality (===) operator to check if the array.length is equal to 0.

If the if statement satisfied the condition, we returned -1 and exited from the indexOfMaximumValue() function; otherwise, we could jump to the else block. In the else block, we declared two variables named maximumValue & maxIndex holding the my_array‘s first element (which is at index 0) and 0, respectively.

Next, we used a for loop, which iterated from 1 to my_array.length-1. In every iteration, we used an if statement with the> operator to check if the array’s current element is greater than the maximumValue; if it is, then update the values of maxIndex and maximumValue.

Once the for loop was over, we returned the index we were updating in maxIndex. Next, we invoked the indexOfMaximumValue() function by passing an array as a parameter, and lastly, we printed the returned result on the console.

Using reduce() Function

To get an index of the maximum value in a JavaScript array:

  • Use the .reduce() function to get the maximum value index from the given array.

Here, we used the .reduce() method to figure out the index of the maximum number from the specified array as an input. The .reduce() function took two arguments; the first was the function, and the second was 0. Let’s understand the function and second argument below:

  • iMax – It was the best index yet, which meant the index of the maximum value so far. On the first iteration, the iMax = 0 because the second argument to the .reduce() function was 0.
    Why is it 0? It is because we assumed that the first element of the array was the maximum number, so we can not omit the second argument in our case.
  • currentValue – The currently tested value from the array.
  • currentIndex – It denotes the currently tested index.
  • arr – It is our array [5,6,7,2,8,0].

The first argument of the .reduce() method performed the reduction operation. The job of this reduction function was to combine/reduce two values into one value and return that reduced value.

Note that the functions used with the .reduce() function are not the same as the functions used with map() and forEach(). The currentValue, currentIndex, and arr were passed as the second, third and fourth parameters. The first parameter was the reduction’s accumulated result so far.

While invoking this function the first time, this first argument was the initial value we passed as the second argument to .reduce(); on subsequent calls, it was the value returned by the function’s previous invocation.

When we invoked the .reduce() function without any initial value, it used the array’s first element as an initial value. So, the first call to the reduction function will have the first and second elements of the array as its first and second arguments, respectively.

Using _.indexOf() with _.max() Method

To get an index of the maximum value in a JavaScript array:

  • Use the require() method to import the Underscore library.
  • Use _.max() to find the maximum number from the given array.
  • Use _.indexOf() to get an index of the maximum number we got from the previous step.

Here, we used Underscore library to simplify things. To take advantage of this library, we used the require() method to import it by specifying the library name as an argument.

After declaring and initializing an array, we used the _.max() method that took the my_array as a parameter. It took the element from the given array one by one and compared the elements to find the greatest number in the my_array. The _max() ended after iterating over and comparing all the array elements.

Next, we used the _.indexOf() method to get an index of the element whose position/index we wanted to find. It started from 0 to count the index of the elements in the my_array. Remember, it will return -1 if unable to find the required element (returned by the _.max(my_array)) in the my_array (which is passed as a first argument).

Until now, we have learned various approaches for finding the maximum value index from an unsorted array. So, let’s see what we can do in a sorted array.

Using sort() with indexOf() Method

To get an index of the maximum value in a JavaScript array:

  • Use the spread operator to create the copy of the array.
  • The copied array from the last step is chained with the .sort() method to sort the array in descending order.
  • After sorting, use .indexOf() to find the index of the first element, which is at index 0.

After declaring and initializing the array, we used the ... to make a copy of the my_array. How? The spread operator turned [5,6,7,2,8,0] to 5,6,7,2,8,0 but wrapping it around [] made it an array again that we stored in the copied_array variable.

Next, we chained copied_array with the sort() method to sort in descending order. Doing so gave us the maximum value at the 0 index. Then, we used .indexOf() to grab the index of the first element, which was at index 0.

That’s all about how to get index of max value in array in JavaScript

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *