Math.abs()
Returns the absolute value of a number.
Syntax
Math.abs(x) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
x | number | Yes | The number whose absolute value is returned. |
Returns
number — The absolute value of x, or NaN if x is not numeric.
Examples
console.log(Math.abs(-5), Math.abs(5));
Output
5 5
console.log(Math.abs('abc'));
Output
NaN
Notes
Non-numeric arguments are coerced; uncoercible values yield NaN. `Math.abs(-0)`
is 0.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |