basic

Math.abs

Returns the absolute value of a number — its distance from zero without regard to sign. Useful for differences, magnitudes, and tolerance checks.

Math.abs(x)

Parameters & edge cases

Case Behavior
x The input number; strings and booleans are coerced
NaN Math.abs(NaN) returns NaN; Math.abs(undefined) also returns NaN
-0 Math.abs(-0) returns 0, collapsing the sign

Examples

console.log(Math.abs(-5));    // 5

Classic absolute value.

console.log(Math.abs(5));     // 5

Positives are returned unchanged.

console.log(Math.abs(-3.14)); // 3.14

Works for floats.

console.log(Math.abs(a - b) < 1e-9); // true/false

Standard floating-point equality within an epsilon tolerance.

Gotcha

For extremely large negatives, Math.abs(Number.MIN_SAFE_INTEGER) is safe, but Math.abs(-Infinity) returns Infinity — verify with Number.isFinite before dividing.

Related

← All JS Math methods · Array methods