1. 逐个指定参数
Input : Math.min(10, 32, 2)
Output : 2
Input : Math.min(-10, -32, -1)
Output : -32
Input : Math.min()
Output : -Infinity
Input : Math.min(10,2,NaN)
Output : NaN
2. 参数为数组
使用 apply
const arr = [2, 3, -5, 15, 7];
Math.min.apply(null, arr); // -5
Math.max.apply(null, arr); // 15
如果是ES6语法,可以使用展开操作符(spread operator) ...
const arr = [2, 3, -5, 15, 7];
Math.min(...arr); // -5
Math.max(...arr); // 15
这个网页很好测试代码:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
- https://www.geeksforgeeks.org/math-min-javascript/
- Learning JavaScript (Ethan Brown)