JavaScript Math min()方法用法介绍

2021年3月13日17:00:27 发表评论 557 次浏览

下面是Math min()方法的示例。

  • 例子:
    filter_none

    编辑

    play_arrow

    链接
    亮度_4
    代码

    <script type= "text/javascript" >
          document.write( "When  positive numbers are passed" +
                         " as parameters: " + Math.min(10, 32, 2));
    </script>

    chevron_right

     
        

    filter_none

     
        

  • 输出如下:
    When  positive numbers are passed as parameters: 2

的Math.min()方法用于返回方法中传递的最低值的数字。如果任何参数都不是数字并且不能转换为一个, 则Math.min()方法将返回NaN。 min()是Math的静态方法, 因此, 它始终用作Math.min(), 而不是用作创建的Math对象的方法。

语法如下:

Math.min(value1, value2, ...)

参数:此方法接受ingle参数, 如上所述, 该参数可以使用n次, 如下所述:

  • 值:此值发送到math.min()找到最大的方法。

返回值:的Math.min()方法返回给定数字中的最小数字。

以下示例说明了JavaScript中的Math min()方法:

  • 范例1:
    Input : Math.min(10, 32, 2)
    Output: 2
  • 范例2:
    Input : Math.min(-10, -32, -1)
    Output: -32
  • 范例3:
    Input : Math.min()
    Output: Infinity
  • 范例4:
    Input : Math.min(10, 2, NaN)
    Output: Nan

上述方法的更多代码如下:

上述方法的更多代码如下:

程序1:

当传递负数作为参数时。

<script type= "text/javascript" >
          document.write( "Result : " + Math.min(-10, -32, -1));
</script>

输出如下:

Result : -32

程式2:没有参数传递时。

<script type= "text/javascript" >
          document.write( "Result : " + Math.min());
</script>

输出如下:

Result : Infinity

程序:NaN作为参数传递时。

<script type= "text/javascript" >
          document.write( "Result : " + Math.min(10, 2, NaN));
</script>

输出如下:

Result : NaN

支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • 歌剧
  • 苹果浏览器

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: