Javascript Math.min( )方法的用法小结

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_参考手册

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值