JavaScript (ES2026) reference · Math Methods and Constants
JavaScript Math.abs() Method
Return the absolute value of a number: its distance from zero, so Math.abs(-5) is 5.
JavaScript (ES2026) Since ES1
Syntax
Math.abs(x)
Parameters
| Parameter | Description |
|---|---|
x |
A number. |
Return value
A non-negative number (or NaN).
Examples
console.log(Math.abs(-7.5), Math.abs(3), Math.abs("-2"), Math.abs("x"));
Output
7.5 3 2 NaN
const a = 0.1 + 0.2, b = 0.3;
console.log(a === b, Math.abs(a - b) < Number.EPSILON);
Output
false true
Version history
| Version | Change |
|---|---|
| JavaScript ES1 | Added |
In other languages
abs()· Python
Related
Math.sign()· Return 1, -1 or 0 depending on whether a number is positive, negative or zero.Number.EPSILON· The smallest difference between 1 and the next representable number, used as a tolerance when comparing floats.- JavaScript Math · tutorial
- JavaScript Numbers · tutorial