Math 对象用于执行数学任务。
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。
js 中Math对象的常用方法
// 1.基本方法:
Math.round(5/2) // 对参数四舍五入为最接近的整数 => 3
Math.ceil(x) // 向上取整,有小数就整数部分加1 Math.ceil(2.2) => 3, Math.ceil(2.9) => 3
Math.floor(5/2) // 对参数进行下舍入为整数 => 2
Math.pow(x, y) // 返回 x 的 y 次幂 Math.pow(2, 5) => 32
Math.random() // 返回 0 ~ 1 之间的随机数
Math.max(x, y) // 返回给定参数中的最大值, 参数可以是多个值 Math.max(0, 160, 35, 20, -8, -100) => 160
Math.min(x, y) // 返回给定参数中的最小值,参数可以是多个值 Math.min(0, 160, 35, 20, -8, -100) => 100
// 2.组合方法:
const numbers = [5, 6, 2, 3, 7]
Math.max.apply(null, array) // 返回数组的最大值
Math.min.apply(null, array) // 返回数组的最小值
参考来源:https://www.w3school.com.cn/jsref/jsref_obj_math.asp