While working in javascript arrays, often there is a requirement to get the minimum element of an array. This article demonstrates easy ways to get the minimum element out of all the elements in the array using different methods and example illustrations.
Table of Contents:
- Get min element of an array using Math.min()
- Get min element of an array using reduce()
- Get min element of an array using iteration
Get min element of an array using Math.min()
Javascript’s Math.min() method will return the lowest 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 minimum element in the array [78,89,54,3,901,1177].
Frequently Asked:
Code:-
function getMinValue(_array) { //Math.min() returns the minimum value out of all the numbers passed in as parameters return Math.min(..._array); }; //usage of the function let myArray = [78,89,54,3,901,1177]; console.log(getMinValue(myArray));
Output:-
3
Explanation:-
- Here, Math.min() method is used to get the minimum value of all the elements in the array.
Get min 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 minimum element in the array [78,89,54,3,901,1177].
Code:-
// using reduce function getMinValue(_array) { return _array.reduce(function (previousValue, nextValue) { // compare two elements of the array using reduce and the smallest out of the two //is passed as argument for next comparison return ( previousValue < nextValue ? previousValue : nextValue ); }); } //usage of the function let myArray = [78,89,54,3,901,1177]; console.log(getMinValue(myArray));
Output:-
3
Explanation:-
- Here, within the reduce() method, a callback function is applied on each array element that compares two elements and returns the minimum value out of those two. This smallest value result is passed as a parameter (previousValue) to be compared with the next element of the array (nextValue).
- Finally, the smallest element of the array gets returned.
Get min element of an array using iteration
Example:-
Get the minimum element in the array [78,89,54,3,901,1177].
Code:-
// using iteration function getMinValue(_array) { var length = _array.length; var minValue = Infinity; // iterate in a while loop // first comparison is done with Infinity while (length--) { //If value smaller than minValue is found, minValue is assigned with that value. if (_array[length] < minValue) { minValue = _array[length]; } } return minValue; }; //usage of the function let myArray = [78,89,54,3,901,1177]; console.log(getMinValue(myArray))
Output:-
3
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 minValue.
- While iterating, as soon as the next smaller element is found, this smaller value is assigned to the variable minValue.
- Finally, the smallest element of the array is assigned to minValue and this minValue is 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 minimum element in an array. Good Luck !!!