4. Finding the Minimum and Maximum Value in an Array
One of the common searches we do in an array is to find the minimum and maximum value. In JavaScript, there are no built-in functions to find the maximum or the minimum value in an array. To do this, you can sort the array first and use the index to look for the highest and lowest values. However, this method seems to be inefficient.
Using the Math.max.apply() and Math.min.apply(), you can search for the maximum and minimum values, respectively.
For example:
<body>
<h1> Min and Max Value </h1>
<button onclick="findMin(points)">Find Minimum</button>
<button onclick="findMax(points)">Find Maximum</button>
<p id="demo"></p>
<script>
var points = [9, 3, 52, 23, 63, 98, 76];
function findMin(arr) {
var min = Math.min.apply(null, arr);
document.getElementById("demo").innerHTML = min;
}
function findMax(arr) {
var max = Math.max.apply(null, arr);
document.getElementById("demo").innerHTML = max;
}
</script>
</body>

The Math.max() and Math.min() do not take arrays. It only gets comma separated arguments. Thus, Math.max.apply() and Math.min.apply() were used. These two methods will require two arguments – the this argument and the array. For now, the value used in the first argument does not matter as it will give the same result if we use the values Math, "" or 0. The second argument will take the array that we want to get the minimum and maximum values.