JavaScript Math.min() 方法用于返回方法中传递的最低值的数字。如果任何参数不是数字并且无法转换为数字,则Math.min() 方法将返回NaN。 min() 是Math的静态方法,因此,它始终用作Math.min(),而不是作为创建的Math对象的方法。
也就是说由于
min()
是Math
的静态方法,所以应该像这样使用:Math.min()
,而不是作为你创建的Math
实例的方法(Math 不是构造函数)。
另外,如果没有参数,结果为Infinity
。如果有任一参数不能被转换为数值,结果为NaN
。
语法:
Math.min(value1, value2, ...)
参数:此方法接受发送到Math.min() 方法的Value1,Value2......一组数值,以查找最小值的数字。
返回值:Math.min() 方法返回给定数字中的最小数字。
下面是 Math.min() 方法的示例。
示例1:当正数作为参数传递时。
<script>
document.write("Output : " + Math.min(10, 32, 2));
// console.log("Output : " + Math.min(10, 32, 2));
</script>
输出:
Output : 2
示例2:当负数作为参数传递时。
<script>
document.write("Output : " + Math.min(-10, -32, -1));
// console.log("Output : " + Math.min(-10, -32, -2));
</script>
输出:
Output : -32
示例3:未传递任何参数时。
<script type="text/javascript">
document.write("Output : " + Math.min());
// console.log("Output : " + Math.min());
</script>
输出:
Output : -Infinity
示例4:当NaN作为参数传递时。
<script>
document.write("Output : " + Math.min(10,2,NaN));
// console.log("Output : " + Math.min(10,2,NaN));
</script>
输出:
Output : NaN
支持的浏览器
下面列出了JavaScript Math.min() 方法支持的浏览器:
- 谷歌浏览器 1 及以上版本
- IE浏览器 3 及以上版本
- 火狐浏览器 1 及以上版本
- Opera浏览器 3 及以上版本
- Safari浏览器 1 及以上版本
参考资料:Math.min_Math_JavaScript_参考手册