CKTIS / JavaScript
Math.sign: numeric sign
Math.sign converts its argument to Number and returns -1, +1, the corresponding zero, or NaN according to its sign.
Version: ECMAScript 2015 onward; example target: Node.js 24.22.2
Syntax
Math.sign(x)Math.sign(x) applies ToNumber. It returns -1 for a negative non-zero value and +1 for a positive non-zero value, including -Infinity and Infinity. For NaN, +0, and -0, it returns the argument itself, so the zero sign is preserved.
Ordinary display often renders both zeros as 0; detect negative zero with Object.is(Math.sign(x), -0) or a reciprocal. A missing argument converts from undefined to NaN. A numeric string is converted, while a Symbol or BigInt throws TypeError; an object can run conversion code.
The function has been standard since ECMAScript 2015 and is not a constructor. It is useful for direction, comparison normalization, and branching, but discards magnitude. Do not treat the result as a simple Boolean: -1 and +1 are truthy, both zeros are falsy, and NaN is falsy too.
Examples
console.log([-7, 0, 9, Infinity, -Infinity].map(Math.sign).join(","));
console.log(Object.is(Math.sign(-0), -0), Math.sign("-12"));
console.log(Number.isNaN(Math.sign()), Number.isNaN(Math.sign(NaN)));
try { Math.sign(3n); } catch (error) { console.log(error instanceof TypeError); }Result
-1,0,1,1,-1 true -1 true true true
Math.sign → -1, 0, +1 and preserved -0