How do I find the largest number from a given array of n elements in javascript


I have a Javascript array with n elements, is there any function to find the largest element of all?

1 Answer

4 years ago by

You can use the below code to find the largest number present in an array.

var largest = array.reduce(function(x,y) {
    return (x > y) ? x : y;
});

The variable largest will be the largest number of the given array.

For your reference, check the program here

4 years ago by Divya