Math.max()
Math.max()
函数返回一组数中的最大值。
尝试一下
语法
Math.max(value1[,value2, ...])
参数
value1, value2, ...
- 一组数值
返回值
返回给定的一组数字中的最大值。如果给定的参数中至少有一个参数无法被转换成数字,则会返回 NaN
。
描述
示例
使用 Math.max()
Math.max(10, 20); // 20 Math.max(-10, -20); // -10 Math.max(-10, 20); // 20
下面的方法使用 apply
方法寻找一个数值数组中的最大元素。getMaxOfArray([1,2,3])
等价于 Math.max(1, 2, 3)
,但是你可以使用 getMaxOfArray()
作用于任意长度的数组上。
function getMaxOfArray(numArray) {
return Math.max.apply(null, numArray);
}
或者通过使用最新的扩展语句spread operator
,获得数组中的最大值变得更容易。
var arr = [1, 2, 3];
var max = Math.max(...arr);
规范
规范版本 | 规范状态 | 注解 |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) Math.max |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Math.max |
Standard | |
ECMAScript (ECMA-262) Math.max |
Living Standard |
浏览器兼容性
BCD tables only load in the browser