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

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

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

内容目录

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

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

语法

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

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

参数

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

返回值

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

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

示例&说明

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

// 两个参数,返回较小的数
document.writeln( Math.min( -3, -5 ) );	// -5

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

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

//传递的参数中有NaN值
document.writeln( Math.min( "李四", 10.2, 5 ) );	// NaN

运行代码

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

0 条评论

撰写评论

打开导航菜单