The following example code demonstrates How to Find the Minimum and Maximum from an Array in JavaScript.

In order to find the maximum and minimum value from the array, first we need to create the array. So, the first line in the code creates the array arr. Also, it initializes the array with integer values. After that, we use the for loop to display the array elements. The variables min and max are assigned the first element of the array. Further, the loop iterates from the second element to the last element.

When an element encounters that is smaller than min, we set the min to that element. Similarly, we update the value of max when any element larger than max is found. So, when the loop terminates, the min and max variables hold the minimum and maximum values respectively.

<html>
  <head>
    <title>Minimum and Maximum</title>
    <script>
	arr=[12, 9, -20, 11, 100, 56, 20, -523, 68, 32];
    	document.write("Array Elements:  <br>");
	for(i=0;i<arr.length;i++)
    		document.write(arr[i]+"  ");
        min=arr[0];
        max=arr[0];
	for(i=1;i<arr.length;i++)
	{
		if(min>arr[i])
			min=arr[i];
		if(max<arr[i])
			max=arr[i];	
	}
	document.write("<br>Minimum = "+min);
	document.write("<br>Maximum = "+max);
    </script>
  </head>
  <body>
  </body>
</html>

Output

The Output of the program demonstrates How to Find the Minimum and Maximum from an Array in JavaScript
The Output of the program demonstrates How to Find the Minimum and Maximum from an Array in JavaScript

Further Reading

HTML Practice Exercise

Evolution of JavaScript from ES1 to ES2020

Introduction to HTML DOM Methods in JavaScript

JavaScript Practice Exercise

Understanding Document Object Model (DOM) in JavaScript

Understanding HTTP Requests and Responses

What is Asynchronous JavaScript?

JavaScript Code for Event Handling

Princites

IITM Software Development Cell