How to Get Index of Greatest Value in an Array in JavaScript ?
Last Updated :
23 Aug, 2024
In this article, we will see how to return the index for the greatest value in an array with the help of JavaScript. To return the index of the greatest value in an array using JavaScript.
Some of the examples are to return the index of the greatest value in an array.
Example: The greatest value in the array is 8, which is located at index 3.
Input: arr = [2, 5, 1, 8, 3];
Output: 3
Example: The greatest value in the array is -1, which is located at index 2.
Input: arr =[-2, -5, -1, -8, -3];
Output: 2
Using for loop: We can use a simple for loop to iterate through the array and keep track of the index of the greatest value seen so far.
Approach:
- We declare a function that takes an array arr as a parameter.
- We initialize a variable maxIndex to 0, which will hold the index of the greatest value.
- We loop through the array using a for loop, starting at index 1 (We have already set maxIndex to 0).
- For each element in the array, we check if it is greater than the current maximum value (the value at arr[maxIndex]).
- If it is greater, we update the maxIndex variable to the current index i.
- We return maxIndex after the loop.
Example 1: In this example, the greatest value in the array is 8, which is located at index 3.
JavaScript
function indexOfMax(arr) {
let maxIndex = 0;
for (let i = 1; i < arr.length; i++) {
if (arr[i] > arr[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
let arr = [2, 5, 1, 8, 3];
console.log(indexOfMax(arr));
Output:
3
Example 2: In this example, the greatest value in the array is -1, which is located at index 2.
JavaScript
function indexOfMax(arr) {
let maxIndex = 0;
for (let i = 1; i < arr.length; i++) {
if (arr[i] > arr[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
let arr = [-2, -5, -1, -8, -3];
console.log(indexOfMax(arr));
Output:
2
Using the reduce() method: We can use the reduce() method to find the index of the greatest value in an array.
Approach:
- We declare a function that takes an array arr as a parameter.
- We call the reduce() method on the array arr.
- The reduce() method takes a callback function with four parameters:
- maxIndex is the index of the current maximum value seen so far (initialized to 0).
- elem is the current element being processed.
- i is the index of the current element being processed.
- arr is the original array being processed.
- In the callback function, we check if the current element elem is greater than the current maximum value (arr[maxIndex]).
- If it is greater, we return the current index i, which becomes the new maxIndex.
- If it is not greater, we return the maxIndex variable.
- We return the final maxIndex after the function call.
Example 3: In this example, the greatest value in the array is 8, which is located at index 3.
JavaScript
function indexOfMax(arr) {
return arr.reduce((maxIndex, elem, i, arr) =>
elem > arr[maxIndex] ? i : maxIndex, 0);
}
let arr = [2, 5, 1, 8, 3];
console.log(indexOfMax(arr));
Output:
3
Example 4: In this example, the greatest value in the array is -1, which is located at index 2.
JavaScript
function indexOfMax(arr) {
return arr.reduce((maxIndex, elem, i, arr) =>
elem > arr[maxIndex] ? i : maxIndex, 0);
}
let arr = [-2, -5, -1, -8, -3];
console.log(indexOfMax(arr));
Output:
2
Using Math.max()
First, we use the Math.max() function to determine the maximum value in the array. Then, we use the map() method to transform the array into an array of indices where the maximum value occurs. Finally, we use the indexOf() method to find the first occurrence of the maximum value’s index.
JavaScript
function findMaxIndex(arr) {
const maxValue = Math.max(...arr);
return arr.indexOf(maxValue);
}
const arr1 = [2, 5, 1, 8, 3];
console.log(findMaxIndex(arr1));
const arr2 = [-2, -5, -1, -8, -3];
console.log(findMaxIndex(arr2));
Similar Reads
JavaScript - Find Index of a Value in Array Here are some effective methods to find the array index with a value in JavaScript.Using indexOf() - Most Used indexOf() returns the first index of a specified value in an array, or -1 if the value is not found. JavaScriptconst a = [10, 20, 30, 40, 50]; // Find index of value 30 const index = a.inde
2 min read
Find Second Smallest Element in an Array in JavaScript JavaScript allows us to compute the Second Smallest Element within a given array. The task involves iterating through the array, identifying the smallest and second smallest element in the array. There are several approaches to finding the second smallest element in an array using Javascript which a
2 min read
How to Get Index of the Max Value in Array of Objects ? When dealing with arrays of objects in JavaScript, it's common to need the index of the object with the maximum value based on a certain property. Below are the approaches to get the index of the max value in an array of objects: Table of Content Using a LoopUsing Array.reduce() MethodUsing a LoopTh
2 min read
How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort
3 min read
Max/Min value of an attribute in an array of objects in JavaScript In this article, we are given an array of objects and the task is to get the maximum and minimum values from the array of objects. For this approach, we have a few methods that will be discussed below. Methods to get Min/Max values:using JavaScript apply() and Array map()using JavaScript Array reduc
3 min read
How to Select Min/Max Dates in an Array Using JavaScript? Here are the different ways to select min/max dates in an array using JavaScript1. Using Math.max and Math.minUse Math.max and Math.min function to get the maximum and minimum dates respectively.JavaScriptlet dates = [ new Date("2019/06/25"), new Date("2019/06/26"), new Date("2019/06/27"), new Date(
3 min read
Sort An Array Of Arrays In JavaScript The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [
2 min read
How to Get the Longest String in an Array using JavaScript? Given an array, the task is to get the longest string from the array in JavaScript. Here are a few of the most used techniques discussed with the help of JavaScript. In this article, we will use JavaScript methods to find out the longest string in the array. All approaches are described below with e
5 min read
JavaScript - Max Element in an Array These are the following ways to find the largest element in JS array: Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the largest element among them. 1. Using the Spread Ope
4 min read
Find the min/max element of an Array using JavaScript To find the minimum or maximum element in a JavaScript array, use Math.min or Math.max with the spread operator.JavaScript offers several methods to achieve this, each with its advantages.Using Math.min() and Math.max() Methods The Math object's Math.min() and Math.max() methods are static methods t
2 min read