Latest web development tutorials
 

JavaScript abs() Method

< JavaScript Math Object

Example

Return the absolute value of a number:

Math.abs(-7.25);

The result will be:

7.25
Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

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


Browser Support

Method
abs() Yes Yes Yes Yes Yes

Syntax

Math.abs(x)

Parameter Values

Parameter Description
x Required. A number

Technical Details

Return Value: A Number, representing the absolute value of the specified number, or NaN if the value is not a number, or 0 if the value is null
JavaScript Version: 1.0

Examples

More Examples

Example

Return the absolute value of different numbers:

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);

The result of a,b,c,d, and e will be:

7.25
7.25
0
NaN
5
Try it Yourself »

< JavaScript Math Object