JavaScript abs()Method
Example
Definition and Usage
The abs() method returns the absolute value of a number.
Browser Support
![]()
![]()
![]()
![]()
![]()
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);
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
7.25
0
NaN
5
Try it yourself »
Other Extensions
JavaScript Math Object