JavaScript Math.abs() Method
The Math.abs() static method returns the absolute value of a number: its distance from zero, without a minus sign.
Description
The Math.abs() static method returns the absolute value of a number, so negative numbers become positive and positive numbers stay the same. It's used to measure the difference between two values regardless of which is larger.
Math.abs(-0) returns 0, Math.abs(-Infinity) returns Infinity, and anything that can't be converted to a number returns NaN. Numeric strings are converted first, so Math.abs('-3') is 3.
A common use is checking whether two numbers are close enough to count as equal, as in Math.abs(a - b) < 0.001, which avoids problems with floating point rounding. Like every member of Math, it's called on the Math object itself rather than on a number, and it only works with the Number type: passing a BigInt throws a TypeError.
Syntax
Math.abs(x)Parameters
- xA number.
Return Value
The absolute value of x.
Example
Browser Support
The following information will show you the current browser support for the Math.abs() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





