Search code snippets, questions, articles...

Find the index of Max value in an array using Javascript

We will be discussing how to find the index of the maximum value in an array using Javascript. This is a simple task that can be accomplished by using the built-in Math.max() function. However, this function only returns the maximum value, not the index. To get the index, we will use Array.indexOf() function.

If you have an array and you want to get the index of maximum value using Javascript code, you can use one of the methods explained in this post.

Solution 1: Using Math.max(), indexOf() and spread operator

In order to find the index of the max value in an array using Javascript, we can use the Math.max() function in combination with the Array.indexOf() function and the spread operator.

The spread operator allows us to spread the elements of an array out into individual values, which makes it easier to work with them. By using Math.max() and indexOf() together, we can easily find the index of the max value in an array.

Syntax

Array.indexOf(Math.max(...Array));

Code example

// create an array
const arr = [30, 20, 50, 70, 10, 40, 17];
console.log("Array is: ", arr);

// find the max value
const max_val = Math.max(...arr);

// find the index of highest value
const max_index = arr.indexOf(max_val);

console.log("Max value index is: " + max_index);

Output

Array is: [30,20,50,70,10,40,17]
Max value index is: 3

Live Demo

We can write this in one line using the below code:

arr.indexOf(Math.max(...arr));

This code defines an array called arr and assigns it integer values. Then, the max value in the array is found using the Math.max() function. Finally, the index of the max value is found using the indexOf() function.

If you do not want to use the spread operator inside Math.max() function, you can use Math.max.apply() function and pass null as the first parameter and array as the second parameter.

const arr = [30, 20, 50, 70, 10, 40, 17];

const max_value = Math.max.apply(null, arr);

const max_index = arr.indexOf(max_value);

console.log("Max index is: " + max_index);

Output

Max index is: 3

Live Demo

Solution 2: Find the index of max value in the array using For loop

If you need to find the index of the max value in an array, you can do so using a for loop in Javascript. This method will loop through the array and compare each value to the current max value. If the value is greater than the current max, it will update the max and its index.

This process will continue until the end of the array is reached, at which point the index of the max value will be returned.

const arr = [30, 20, 50, 70, 10, 40, 17];

let max_val = arr[0];
let max_index = 0;

for (let i=0; i < arr.length; i++) {
    if (arr[i] > max_val) {
        max_val = arr[i];
        max_index = i;
    }
}

console.log("Maximum value is: " + max_val);
console.log("Maximum value index is: " + max_index);

Output

Maximum value is: 70
Maximum value index is: 3

Live Demo

This code is an example of how to find the maximum value in an array. It starts by setting the maximum value to the first value in the array, and the maximum value index to 0.

It then loops through the array, and if it finds a value that is greater than the maximum value, it sets the maximum value to that value and sets the maximum value index to the index of that value.

Finally, it prints the maximum value and the maximum value index to the console.

For higher performance, use the For loop methods to find the maximum value index from the array.

Solution 3: Using Array.reduce() function

In order to find the index of the max value in an array, the array.reduce() function can be used. This function takes in a callback function as an argument and applies that function to each element in the array, in order to reduce the array to a single value.

In this case, the callback function would be one that compares the current element to the max value seen so far, and if the current element is greater than the max value, the index of that element is returned.

Code example

const numbers = [10, 30, 60, 90, 50, 100, 21];

const max_val_index = numbers.reduce((max_index, val, i, arr) => 
    val > arr[max_index] ? i : max_index, 0
);

console.log("Maximum value index is: " + max_val_index);

Output

Maximum value index is: 5

Live Demo

The code above is an example of finding the index of the maximum value in an array using the reduce() method. The reduce() method takes an array and reduces it down to a single value. In this case, we are reducing it down to the index of the maximum value.

Was this helpful?
0 Comments