This article will show you how to get the min/max elements from an array in JavaScript. The minimum and maximum elements in an array can be found using following methods:
Methods to find min/max elements of an array:
- using Math.min() and Math.max() Methods
- by Iterating through the Array
- using JavaScript Array.reduce() method
Method 1: Using Math.min() and Math.max() Methods
The Math object’s min() and max() methods are static methods that return the minimum and maximum elements of a given array. The spread(…) operator could pass these functions into an array. The spread operator allows an iterable to expand in places where multiple arguments are expected. In this case, it automatically expands the array and gives the numbers to the functions.
Syntax:
minValue = Math.min(...array);
maxValue = Math.max(...array);
Example: In this example, we will see the basic implementation of JavaScript Math.min() and Math.max() methods.
Javascript
function findMinMax() { let Arr = [50, 60, 20, 10, 40]; let minValue = Math.min(...Arr); let maxValue = Math.max(...Arr); console.log( "Minimum element is:" + minValue); console.log( "Maximum Element is:" + maxValue); } findMinMax() |
Minimum element is:10 Maximum Element is:60
Method 2: Iterating through the Array
Iterating through the array and keeping track of the minimum and maximum elements. The minimum and maximum element can be kept track by iterating through all the elements in the array and updating the minimum and maximum element up to that point by comparing them to the current minimum and maximum values. The minimum and maximum values are initialized to Infinity and -Infinity.
Syntax:
minValue = Infinity;
maxValue = -Infinity;
for (item of array) {
// Find minimum value
if (item < minValue)
minValue = item;
// Find maximum value
if (item > maxValue)
maxValue = item;
}
Example: In this example, we will be iterating through the elements of the array to find the max and min elements from the array.
Javascript
function findMinMax() { let Arr = [50, 60, 20, 10, 40]; let minValue = Infinity; let maxValue = -Infinity; for (let item of Arr) { // Find minimum value if (item < minValue) minValue = item; // Find maximum value if (item > maxValue) maxValue = item; } console.log( "Minimum element is:" + minValue); console.log( "Minimum element is:" + maxValue); } findMinMax(); |
Minimum element is:10 Minimum element is:60
Method 3: Using JavaScript Array.reduce() method
The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
Syntax:
array.reduce( function(total, currentValue, currentIndex, arr),
initialValue )
Example:
Javascript
// Input array let Arr = [50, 60, 20, 10, 40]; // Getting min value let minValue = Arr.reduce((acc, current) => Math.min(acc, current)); // Getting max value let maxValue = Arr.reduce((acc, current) => Math.max(acc, current)); // Display output console.log( "Minimum element is:" + minValue); console.log( "Minimum element is:" + maxValue); |
Minimum element is:10 Minimum element is:60