You can find the largest number in an array in #JavaScript with a single function call and without writing any loops
Just call Math.max(...yourArray) to get the largest number in an array of numbers. Math.max is built-in JavaScript function that returns the maximum value of its arguments. Here we're using the spread operator to call Math.max with our array as arguments
Math.max(...yourArray) works as long as the array is reasonably sized. It will throw for very larger arrays.
To find the max value in a very large array, instead use reduce:
yourArray.reduce(function(p, c) { return Math.max(p, c) }, -Infinity)
This will work even for arrays with millions of elements
Ещё видео!