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

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


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