Return the maximum value in an array of numbers.
Use Math.max with spread for small arrays, or a single reduce/loop for very large arrays (spread can hit argument-count limits on huge arrays).
const max = (arr) => Math.max(...arr);
// Safe for very large arrays:
function maxSafe(arr) {
let m = -Infinity;
for (const n of arr) if (n > m) m = n;
return m;
}Finding a max/min is a building block for stats dashboards and for validating that a test dataset's values fall in range.
Math.max(...bigArray) throws 'Maximum call stack size exceeded' for a 200k-element array. Why, and what's the fix?