您的浏览器过于古老 & 陈旧。为了更好的访问体验, 请 升级你的浏览器
Ready 发布于2014年08月05日 02:37

原创 JavaScript Math.max() 函数详解

5350 次浏览 读完需要≈ 4 分钟

内容目录

Math.max()函数用于返回一个或多个数中最大的数

该函数属于JavaScript的内置全局对象Math所有主流浏览器均支持该函数

语法

静态函数Math.max()的语法如下:

Math.max( [ num1 [, nums...] ] )

参数

参数 描述
num1 可选参数/Number类型指定的数
nums 可选参数/Number类型指定的数,可以有多个

返回值

Math.max()的返回值为Number类型。返回所有参数中最大的数。

如果没有传入任何参数,则返回负的无穷大(-Infinity);如果参数中存在为NaN的值,则返回NaN

示例&说明

// 一个参数,就返回该数
document.writeln( Math.max( 2 ) );	// 2

// 两个参数,返回较大的数
document.writeln( Math.max( -3, -3 ) );	// -3

// 多个参数时,返回最大的数
document.writeln( Math.max( -1, 2.1, 10.5, 7 ) );	// 10.5

// 没有传递任何参数
document.writeln( Math.max( ) );	// -Infinity

//传递的参数中有NaN值
document.writeln( Math.max( "张三", 10, 5 ) );	// NaN

运行代码

  • CodePlayer技术交流群1
  • CodePlayer技术交流群2

0 条评论

撰写评论

打开导航菜单