Latest web development tutorials

JavaScript abs () method

Math Object Reference JavaScript Math Object

Examples

Examples

Returns the absolute value of a number:

Math.abs(-7.25);

Output:

7.25

try it"

Definition and Usage

abs () method returns the absolute value of a number.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support abs () method


grammar

Math.abs( x )

Parameter Value

参数 描述
x 必需。必须是一个数值。

return value

类型 描述
Number x 的绝对值。如果 x 不是数字返回 NaN,如果 x 为 null 返回 0。

technical details

JavaScript version: 1.0


More examples

Examples

In this case, I will get the absolute value of an array of different types:

var a=Math.abs(7.25);
var b=Math.abs(-7.25);
var c=Math.abs(null);
var d=Math.abs("Hello");
var e=Math.abs(2+3);

Output:

7.25
7.25
0
NaN
5

try it"


Math Object Reference JavaScript Math Object