JavaScript Math max()方法使用介绍

2021年3月11日17:13:19 发表评论 666 次浏览

以下是示例数学最大值方法。

  • 例子:
    filter_none

    编辑

    play_arrow

    链接
    亮度_4
    代码

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

    chevron_right

     
        

    filter_none

     
        

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

Math.max()方法用于返回零个或多个数字中的最大值。如果未传递任何参数, 则结果为" -Infinity", 如果至少一个参数不能转换为数字, 则结果为NaN。

max()是Math的静态方法, 因此, 始终将其用作Math.max(), 而不是用作创建的Math对象的方法。

语法如下:

Math.max(value1, value2, ...)

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

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

返回线索:的Math.max()方法返回给定数字中的最大数字。

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

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

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

程序1:

当传递负数作为参数时。

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

输出如下:

Result : -1

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

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

输出如下:

Result : -Infinity

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

<script type= "text/javascript" >
          document.write( "Result : " + Math.max(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: