Javascript: Get max element of an array

While working in javascript arrays, often there is a requirement to get the maximum element of an array. This article demonstrates easy ways to get the maximum element out of all the elements in the array using different methods and example illustrations.

Table of Contents:

Get max element of an array using Math.max()

Javascript’s Math.max() method will return the largest value out of all the numbers passed in as a parameter. NaN is returned if parameter passed in the method is not a number.

Example:-

Get the maximum element in the array [78,89,54,3,901,1177].

Code:-

function getMaxValue(_array) 
{
//Math.max() returns the largest value out of all the values passed as argument
  return Math.max(..._array);
};
//usage of the function
let myArray = [78,89,54,3,901,1177];
console.log(getMaxValue(myArray));

Output:-

1177

Explanation:-

  • Here, Math.max() method is used to get the maximum value of all the elements in the array.

Get max element of an array using reduce()

Javascript’s reduce() method will execute a callback function called reducer on each array element that passes the return value from the preceding calculation. The result after applying the reducer on all array elements is a single value. 

Example:-

Get the maximum element in the array [78,89,54,3,901,1177].

Code:-

using reduce
function getMaxValue(_array)
{ 
// compare two elements of the array using reduce and the largest out of the two is passed as argument for next comparison
  return _array.reduce(function (previousValue, nextValue) 
  {
    return ( previousValue > nextValue ? previousValue : nextValue );
  });
}
//usage of the function
let myArray = [78,89,54,3,901,1177];
console.log(getMaxValue(myArray))

Output:-

1177

Explanation:-

  • Here, within the reduce() method, a callback function is applied on each array element that compares two elements and returns the maximum value out of those two. This value is passed as a parameter (previousValue) to be compared with the next element of the array (nextValue).
  • Finally, the largest element of the array gets returned.

Get max element of an array using iteration

Example:-

Get the maximum element of the array [78,89,54,3,901,1177].

Code:-

// using iteration
function getMaxValue(_array) 
{
  var length = _array.length;
  var  maxValue = -Infinity;
// iterate in a while loop
// first comparison is done with -Infinity
  while (length--) 
  {
    if (_array[length] > maxValue) 
    {
//If value larger than maxValue is found, maxValue is assigned with that value.
      maxValue = _array[length];
    }
  }
  return maxValue;
};
// usage of the function
let myArray = [78,89,54,3,901,1177];
console.log(getMaxValue(myArray))

Output:-

1177

Explanation:-

  • In the above code, while loop is used to iterate the array elements and compare them. The comparison starts with Infinity assigned in a variable maxValue.
  • While iterating, as soon as the next bigger element is found, this bigger value is assigned to maxValue.
  • Finally, the largest element of the array is assigned to maxValue and gets returned.

Read More:

I hope this article helped you to get the maximum element of an array. Good Luck !!!

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top