JavaScript abs()Method

Math 对象参考手册JavaScript Math Object

Example

Example

Return the absolute value of a number:

Math.abs(-7.25);

Output:

7.25

Try it yourself »

Definition and Usage

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


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the abs() method


Syntax

Math.abs(x)

Parameter Values

Parameter Description
x Required. Must be a numeric value.

Return Value

Type Description
Number The absolute value of x. If x is not a number, returns NaN; if x is null, returns 0.

Technical Details

JavaScript Version: 1.0


More Examples

Example

In this example, I will get the absolute values of different types of arrays:

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 yourself »


Math 对象参考手册JavaScript Math Object

Other Extensions