Get Max Value From Array in JavaScript

Get Max Value From Array in JavaScript | The largest of the integers provided as input parameters, or -Infinity if there are none, are returned by the Math.max() function. We can use Math.max() function to get the largest value from the given array.

Parameters:-
value1, value2, …, valueN

There may be zero or more numbers, and the largest value will be chosen and returned.

Return value:- the most significant number provided. If any of the parameters is or is changed to a NaN value, NaN is returned. In the absence of any parameters, it returns -Infinity.

We always use max() as Math because it is a static method of Math. Instead of being a method of a Math object, we constructed, max() (Math is not a constructor).

Math.max.length is set to 2, which is a hazy indication that it can accommodate at least two parameters.

Syntax:-

  • Math.max()
  • Math.max(value0)
  • Math.max(value0, value1)
  • Math.max(value0, value1, …, valueN)

In JavaScript, find the index of the maximum value in an array. To find the maximum value’s index in an array:-

  • To find the maximum value in the array, use the Math.max() method.
  • To find the index of the maximum value, use the String.indexOf() method.
  • The indexOf() method returns either -1 if the value cannot be found or the index of the value in the array.

Get Max Value From Array in JavaScript Using Math.max()

const arr = [22, 30, 194, 140, 165];
const max = Math.max(...arr);
console.log("Max value in the given array: " + max);

const index = arr.indexOf(max);
console.log("Index of max value: " + index);

Output:-

Max value in the given array: 194
Index of max value: 2

We utilized the Math.max() method to determine the maximum value in the array. We can’t just send an array to the Math.max() method; it requires multiple, comma-separated values as parameters.

const max = Math.max(2, 3, 4, 145, 16);
console.log(max);

Output:-

145

We unpacked the array’s values using the spread operator (…) before passing them as several comma-separated parameters to the Math.max() method.

const arr = [2, 34, 4, 14, 16];
const max = Math.max(...arr);
console.log("Max value: " + max);

const index = arr.indexOf(max);
console.log("index of max value: " + index);

Output:-

Max value: 34
index of max value: 1

If the array contains a maximum value more than once then the indexOf() method is going to return the index of the first occurrence of the max value.

const arr = [2, 34, 4, 14, 16, 34, 16];
const max = Math.max(...arr);
console.log("Max value: " + max);

const index = arr.indexOf(max);
console.log("index of max value: " + index);

Output:-

Max value: 34
index of max value: 1

Get Max Value From Array in JavaScript Using Loop

const numbers = [-2, 34, 4, -14, 16, 34, 16];
let maxValue = -Infinity;
for (let i = 0; i < numbers.length; i++) {
  if (maxValue < numbers[i]) {
    maxValue = numbers[i];
  }
}
console.log("Max value: " + maxValue);

Output:-

Max value: 34

Notice that the above two ways of finding max value from array in JavaScript using Loop or Math.max() only for the numeric arrays. Also see:- How to Find the Longest String in an Array in JavaScript

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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