Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript abs() method

JavaScript abs() method

Math Object Reference JavaScript Math object

Example

Example

Return the absolute value of a number:

Math.abs(-7.25);

Output result:

7.25

Try it »


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 value

Parameters Description
x Required. Must be a numeric value.

Return Value

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

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 result:

7.25

7.25

0

NaN

5

Try it »


Math Object Reference JavaScript Math object