Math.min()和Math.max()的使用
Math.min()返回零个或更多个数值的最小值1.返回给定数值中最小的数。如果任一参数不能转换为数值,则返回NaN。如果没有参数,结果为Infinity(无穷大)。参数有效值:整数,浮点数,数字字符串。参数无效值:非数字字符串,空变量,空数组(非数字)。//正常使用console.log(Math.min(-1, 4, 6, 12));//输出值:-1console.log(Math.min(1
文章共400字 · 阅读需要大约2分钟
一键AI生成摘要,助你高效阅读
问答
·
Math.min()返回零个或更多个数值的最小值
1.返回给定数值中最小的数。如果任一参数不能转换为数值,则返回NaN。如果没有参数,结果为Infinity(无穷大)。
参数有效值:整数,浮点数,数字字符串。
参数无效值:非数字字符串,空变量,空数组(非数字)。
//正常使用
console.log(Math.min(-1, 4, 6, 12));//输出值:-1
console.log(Math.min(1.23, 45, 6, 0.1));//输出值:0.1
console.log(Math.min("0.01", "-45"));//输出值:-45
//参数不是数值时
console.log(Math.min("Js", "world"));//输出值:NaN
console.log(Math.min("1", "two"));//输出值:NaN
//没有参数时
console.log(Math.min());//输出值:Infinity
2.Math.min()方法经常用于裁剪一个值,以便使其总是小于或等于某个边界值。
var x = f(foo);
if (x > boundary) {
x = boundary;
}
//等价于
var x = Math.min(f(foo), boundary);
Math.max()返回一组数中的最大值
1.返回给定的一组数字中的最大值。如果给定的参数中至少有一个参数无法被转换成数字,则会返回 NaN。如果没有参数,则结果为 - Infinity。
Math.max(10, 20); // 20
Math.max(-10, -20); // -10
Math.max(-10, 20); // 20
Math.min()比Math.max()大
if(Math.min()<Math.max()){
console.log("true");
}else{
console.log("false");
}
//输出值:false
//因为Math.min() 返回 Infinity, 而 Math.max()返回 -Infinity。
更多推荐
已为社区贡献1条内容
所有评论(0)