How to find the largest number contained in a JavaScript array?

JavascriptWeb DevelopmentFront End Technology

To get the largest number in the array, you can try to run the following code. It returns the largest number 530 −

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var myArr = [340, 25, 530, 299];
         var max = myArr.reduce(function(a,b){
            return (a > b) ? a : b;
         });
         document.write("Largest number in the array: "+max);
      </script>
   </body>
</html>

Output

Largest number in the array: 530
raja
Published on 03-Feb-2018 12:44:58
Advertisements