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

JavascriptWeb DevelopmentFront End Technology

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

Live Demo

<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
raja
Published on 18-Feb-2018 10:52:40
Advertisements