In this tutorial, you will learn how to get or find the minimum/lowest/smallest and maximum/largest/ value or element from an array in javascript using for loop, min(), and max() functions.
How to Find the Min/Max Elements in an Array in JavaScript
First of all, you must know about the for loop, sort(), min(), and max() methods of javascript.
Here are different approaches to finding or getting minimum and maximum value or element from an array in javascript.
Min() method javascript
JavaScript max () method helps us to get or find the lowest or minimum value/elements from a specified array.
Syntax
The syntax of min method is:
Math.min(n1, n2, n3, …, nX)
Max() method javascript
JavaScript max () method helps us to get or find the highest or maximum value/element from a specified array.
Syntax
The syntax of max method is:
Math.max(n1, n2, n3, ..., nX)
Approach 1 – Find min/mininum/smallest value/element from array using Min()
Let’s take the first example, to find the minimum and maximum value in array javascript.
Here is an example:
<script> var numbers = [25, 45, 15, -5, 73, 6]; //find the max value from array javascript var maxValue = Math.max.apply(null, numbers); document.write(maxValue + '<br>'); // output:73 //find the min value from array javascript var minValue = Math.min.apply(null, numbers); document.write(minValue); // output:-5 </script>
Approach 2 – Find Max/maximum/largest elements/value from array
Let’s take the second example, to find the minimum or lowest and maximum or largest value from array in javascript.
Her is an example:
<script> var numbers = [25, 45, 15, -5, 73, 6]; //find the max value from array javascript var maxValue = Math.max(...numbers); document.write(maxValue + '<br>'); // output:73 //find the min value from array javascript var minValue = Math.min(...numbers); document.write(minValue); // output:-5 </script>
Approach 3 – Find min and max in array javascript using for loop
A simple for
loop also helps us to find both the minimum and maximum values.
Here is another approach to finding min and max in an array in JavaScript using a for loop:
function findMinMax(arr) {
var min = arr[0];
var max = arr[0];
for (var i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
return { min: min, max: max };
}
var arr = [1, 2, 3, 4, 5];
var result = findMinMax(arr);
console.log(result); // { min: 1, max: 5 }
Approach 4: Using the sort
Method
The sort
method helps us to sort the array, and then the first and last elements can be extracted as the minimum and maximum values.
Here is another approach to finding minimum and maximum in an array in JavaScript using a sort:
// Example array
const numbers = [3, 1, 7, 4, 2, 8, 6];
// Sorting the array and finding minimum and maximum values
numbers.sort((a, b) => a - b);
const minValue = numbers[0];
const maxValue = numbers[numbers.length - 1];
console.log("Minimum Value:", minValue); // Output: 1
console.log("Maximum Value:", maxValue); // Output: 8
Conclusion
That’s it; you have learned how to find min and max value or element from an array using for loop, reduce(), min(), and max() in javascript.