let max = Math.max([1, 7, 9, 4, 2]) // NaN
max = Math.max.apply(null, [1, 56, 7, 8]) // 56
console.log(max)
补充
补充:
Math.max/Math.min不接受数组(传入数组会被当作一个参数返回 NaN),需要把数组"展开"传成多个参数:
Math.max.apply(null, arr):apply 把数组展开为参数列表;Math.max(...arr):ES6 展开运算符,更直观;arr.reduce((m, v) => v > m ? v : m):纯算术实现,无需Math;arr.sort((a, b) => b - a)[0]:排序后取首(开销大,不推荐)。
const arr = [1, 7, 9, 4, 2]
Math.max(...arr) // 9
arr.reduce((m, v) => v > m ? v : m, -Infinity) // 9
来源整理自:vue3js.cn 面试官系列



