Get The Index Of The Max Value In An Array In JavaScript

To get the index of the max value in an array in JavaScript –

  1. Pass the array using the … operator (spread operator) as an argument to Math.max() method to get the max value.
  2. Use the indexOf() method to get the first index of the max value in the array.
  3. For Internet Explorer, use Math.max.apply() method.
  4. If you do not want to use the Math.max() method, you can use the for loop instead.
get the index of the max value in an array in JavaScript

Math.max() Method In JavaScript

The Math.max() method returns the largest of zero or more numbers. The purpose of this function is to find the maximum value in a set of values. The function takes an arbitrary number of arguments and returns the maximum value. If no arguments are provided, the function returns undefined.

The function does not change the values of the arguments. The Math.max() method can be used with any data type, but it is most commonly used with numbers.

For example, the following code finds the maximum value in an array:

var numbers = [-5, -2, 0, 3, 7];

var max = Math.max(...numbers);

console.log(max); // 7

The … (spread operator) is used to pass the array as a list of arguments to the Math.max() method.

indexOf() Method In JavaScript

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

For example, the following code finds the first index of the max value in an array:

var numbers = [-5, -2, 0, 3, 7];

var max = Math.max(...numbers);

console.log(max); // 7

console.log(numbers.indexOf(max)); // 4

Find The Index Of The Max Value In An Array Using Math.max() And indexOf()

To get the index of the max value of an array in JavaScript, you can use the Math.max() method and the indexOf() method.

The following code finds the index of the max value in an array:

var numbers = [-5, -2, 0, 3, 7];

var max = Math.max(...numbers);

console.log(max); // 7

console.log(numbers.indexOf(max)); // 4

As you can see, the code first finds the max value in the array using the Math.max() method. It then uses the indexOf() method to find the first index of the max value in the array.

The indexOf() method is used because it is possible for there to be more than one max value in an array. If there are multiple max values, the indexOf() method will return the first one.

If you want to find the last index of the max value, you can use the lastIndexOf() method.

For example, the following code finds the last index of the max value in an array:

var numbers = [-5, -2, 0, 3, 7];

var max = Math.max(...numbers);

console.log(max); // 7

console.log(numbers.lastIndexOf(max)); // 4

As you can see, the only difference between this code and the previous code is that the lastIndexOf() method is used instead of the indexOf() method.

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present.

If you want to find all the indices of the max value, you can use the forEach() method.

For example, the following code finds all the indices of the max value in an array:

var numbers = [-5, 7, 0, 3, 7];

var max = Math.max(...numbers);

numbers.forEach((number, index) => {

if (number === max) {

console.log(index);

}

});

Output

1

4

As you can see, the code uses the forEach() method to loop through each element in the array.

If the element is equal to the max value, it prints out the index.

This code will print out all the indices of the max value, even if there are multiple max values.

Find The Index Of The Max Value In An Array Using Math.max.apply() And indexOf()

If you are using Internet Explorer, you can use the Math.max.apply() method to get the index of the max value in an array.

The apply() method calls a function with a given ‘this’ value and arguments provided as an array (or an array-like object).

For example, the following code finds the index of the max value in an array:

var numbers = [-5, -2, 0, 3, 7];

var max = Math.max.apply(null, numbers);

console.log(max); // 7

console.log(numbers.indexOf(max)); // 4

As you can see, the code first finds the max value in the array using the Math.max.apply() method. It then uses the indexOf() method to find the first index of the max value in the array.

The indexOf() method is used because it is possible for there to be more than one max value in an array. If there are multiple max values, the indexOf() method will return the first one.

If you want to find the last index of the max value, you can use the lastIndexOf() method.

For example, the following code finds the last index of the max value in an array:

var numbers = [-5, -2, 0, 3, 7];

var max = Math.max.apply(null, numbers);

console.log(max); // 7

console.log(numbers.lastIndexOf(max)); // 4

As you can see, the only difference between this code and the previous code is that the lastIndexOf() method is used instead of the indexOf() method.

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present.

If you want to find all the indices of the max value, you can use the forEach() method.

For example, the following code finds all the indices of the max value in an array:

var numbers = [-5, 7, 0, 3, 7];

var max = Math.max.apply(null, numbers);

numbers.forEach((number, index) => {

if (number === max) {

console.log(index);

}

});

Output

1

4

As you can see, the code uses the forEach() method to loop through each element in the array.

If the element is equal to the max value, it prints out the index.

This code will print out all the indices of the max value, even if there are multiple max values.

Find The Index Of The Max Value In An Array Without Using Math.max()

If you want to find the index of the max value in an array without using the Math.max() method, you can use a for loop.

For example, the following code finds the index of the max value in an array:

var numbers = [-5, -2, 0, 3, 7];

var max = -Infinity;

var index = 0;

for (let i = 0; i < numbers.length; i++) {

if (numbers[i]>max) {

max = numbers[i];

index = i;

}

}

console.log(max); // 7

console.log(index); // 4

As you can see, the code uses a for loop to loop through each element in the array.

If the element is greater than the max value, it sets the max value to the element and sets the index variable to the index of the element.

After the for loop has finished, the max value and the index of the max value are printed out.

Conclusion

In this article, you learned how to find the index of the max value in an array using various methods.

You also learned how to find the index of the max value without using the Math.max() method.

I hope you found this article helpful.

Happy coding! 🙂

Leave a Reply