Computer >> Computer tutorials >  >> Programming >> Javascript

How to find the min/max element of an Array in JavaScript?


To get the min and max element of an array, use the Math.min() and Math.max() method in JavaScript. You can try to run the following code to find the minimum and maximum element −

Example

<html>
   <body>
      <script>
         var arr = [50, 30, 70, 60];
         document.write("Maximum Value: "+Math.max.apply(null, arr));
         document.write("<br>Minimum Value: "+Math.min.apply(null, arr));
      </script>
   </body>
</html>

Output

Maximum Value: 70
Minimum Value: 30