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
How to Check a Value Exist at Certain Array Index in JavaScript ?
Determining if a value exists at a specific index in an array is a common task in JavaScript. This article explores various methods to achieve this, providing clear examples and explanations to enhance your coding efficiency.Below are the different ways to check if a value exists at a specific index
5 min read
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
How to Check if an Element Exists in an Array in JavaScript?
Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array.
2 min read
How to get a key in a JavaScript object by its value ?
To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 min read
How to Get the Size of an Array in JavaScript
To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that
2 min read
How To Iterate an Array using forEach Loop in JavaScript?
Iterating through an array in JavaScript can be done in multiple ways. One of the most common methods is using the traditional for loop. The modern approach to iterate the array which is by using the forEach method. This method is different from the traditional approach as it uses a functional appro
1 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