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
- Get max element of an array using reduce()
- Get max element of an array using iteration
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:-
Frequently Asked:
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:
- Javascript: Get unique values in an array
- Javascript: Insert an item to array at specific index
- Javascript: Check if an array is empty
- Javascript: Convert array to string (4 ways)
I hope this article helped you to get the maximum element of an array. Good Luck !!!