Math.abs
Returns the absolute value of a number — its value without the sign.
Available since: ES1
Syntax
Math.abs(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | Any number. |
Return value
numberThe number without the minus sign.
Examples
The absolute value of a positive and a negative number:
console.log(Math.abs(-5));
console.log(Math.abs(5));Output:
5
5The difference between two numbers in any order:
const a = 3;
const b = 10;
console.log(Math.abs(a - b));Output:
7Notes
A numeric string is converted automatically: Math.abs('-2') returns 2, and a non-numeric string gives NaN.
See also
Related recipes
In the tutorial
- Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.