Math.abs()
abs(x) Returns:
Number · Added in vES1 · Updated March 13, 2026 · Math javascript math abs absolute
The Math.abs() function returns the absolute value of a number. In other words, it returns the positive version of any numeric value.
Syntax
Math.abs(x)
Parameters
| Parameter | Type | Description |
|---|---|---|
x | Number | A number whose absolute value you want to compute. |
Return Value
- The absolute value of the given number.
- Returns
NaNif the argument isNaNor non-numeric. - Returns
Infinityif the argument isInfinityor-Infinity.
Examples
Basic Usage
Math.abs(5);
// 5
Math.abs(-5);
// 5
Math.abs(0);
// 0
With Different Numeric Types
Math.abs(-3.14);
// 3.14
Math.abs(Infinity);
// Infinity
Math.abs(-Infinity);
// Infinity
With Non-Numeric Values
Math.abs("hello");
// NaN
Math.abs(NaN);
// NaN
Practical Example: Distance Calculation
function distance(x1, y1, x2, y2) {
return Math.sqrt(
Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)
);
}
distance(0, 0, 3, 4);
// 5
distance(1, 2, 1, 2);
// 0
Practical Example: Temperature Range
function temperatureRange(low, high) {
return Math.abs(high - low);
}
temperatureRange(20, 30);
// 10
temperatureRange(30, 20);
// 10
See Also
- Math.sign() — returns the sign of a number
- Math.sqrt() — returns the square root of a number
- Math.pow() — returns the base to the exponent power