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

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

5319 次浏览 读完需要≈ 3 分钟

内容目录

Math.abs()函数用于返回数字的绝对值

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

语法

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

Math.abs( number )

参数

参数 描述
number Number类型需要计算绝对值的数字,返回该数的绝对值

返回值

返回值为Number类型。返回参数number的绝对值。

如果参数number不是或无法转换为Number类型,则返回NaN

示例&说明

// 输出 -5 的绝对值
document.write( Math.abs( -5 ) );	// 5

// 输出 -2.3 的绝对值
document.write( Math.abs( -2.3 ) );	// 2.3

// 输出 0 的绝对值
document.write( Math.abs( 0 ) );	// 0

// 输出 5.2 的绝对值
document.write( Math.abs( 5.2 ) );	// 5.2

// 输出 true 的绝对值
document.write( Math.abs( true ) );	// 1

// 输出 false 的绝对值
document.write( Math.abs( false ) );	// 0

// 输出字符串 "张三" 的绝对值
document.write( Math.abs( "张三" ) );	// NaN

运行代码

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

0 条评论

撰写评论

打开导航菜单