Find and get max and min value in an array of objects javaScript; In this tutorial, you will learn how to get or find the minimum/ lowest/smallest and maximum/largest/ value from an array in javascript.
JavaScript Find Min and Max Value in Array
First of all, you must know about the min() and max() methods of javascript. You can use the min() method to get or find the minimum value from the js array and you can use the max() method to get or find the maximum value from an array in javascript.
Min() method javascript
JavaScript max () method, which is used to get or find the lowest or minimum value from specified values.
Syntax
The syntax of min method is:
Math.min(n1, n2, n3, …, nX)
Max() method javascript
JavaScript max () method, which is used to get or find the highest or maximum value from specified values.
Syntax
The syntax of max method is:
Math.max(n1, n2, n3, ..., nX)
Example 1 – javascript find min and max value in array with the apply()
Method
Let’s take the first example, to find the minimum and maximum value in array javascript. See the example below:
<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>
Example 2 – javascript find min and max value in the array
Let’s take the second example, to find the minimum or lowest and maximum or largest value from array in javascript. See the example below:
<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>