CKTIS / JavaScript
Math.abs: absolute value
Math.abs converts its argument to Number and returns its non-negative absolute value, with defined behavior for zero, NaN, and infinities.
Version: ECMAScript 1 onward; example target: Node.js 24.22.2
Syntax
Math.abs(x)Math.abs(x) applies ToNumber to x, then returns the unsigned magnitude. A finite negative value becomes positive, a positive value is unchanged, and both -0 and +0 produce +0. -Infinity produces Infinity, while NaN remains NaN.
Conversion matters: Math.abs("-3.5") is 3.5, Math.abs(null) is 0, and a missing argument means undefined and yields NaN. An object can invoke its own valueOf or toString, so conversion can have side effects or throw. Symbols and BigInts cannot undergo ToNumber and throw TypeError.
The function is not a constructor and has been standard since ECMAScript 1. Use it for distance from zero or an absolute difference such as Math.abs(a - b), not for multidimensional length—use Math.hypot there. The result is still a Number and remains subject to IEEE 754 precision limits.
Examples
console.log(Math.abs(-12.5), Math.abs("-3"), Math.abs(null));
console.log(Object.is(Math.abs(-0), 0), Math.abs(-Infinity));
console.log(Number.isNaN(Math.abs()), Number.isNaN(Math.abs("not-number")));
const reading = { valueOf() { return -9; } };
console.log(Math.abs(reading));
try { Math.abs(1n); } catch (error) { console.log(error instanceof TypeError); }Result
12.5 3 0 true Infinity true true 9 true