rounding

Math.sign

Returns the sign of a number as 1, -1, 0, -0, or NaN. Introduced in ES2015 as a compact replacement for the ternary `x > 0 ? 1 : x < 0 ? -1 : 0`.

Math.sign(x)

Parameters & edge cases

Case Behavior
x Any numeric value; non-numbers are coerced first
signed zero Math.sign(-0) returns -0, and Math.sign(0) returns 0 — distinct values
NaN Math.sign(NaN) returns NaN — always check with Number.isNaN before using

Examples

console.log(Math.sign(5));    // 1

Positive numbers return 1.

console.log(Math.sign(-5));   // -1

Negative numbers return -1.

console.log(Math.sign(0));    // 0

Positive zero returns 0.

console.log(Math.sign(-0));   // -0

Negative zero returns -0 (===0 is true, but Object.is(-0,0) is false).

Gotcha

Math.sign returns 5 possible values (1, -1, 0, -0, NaN), not just three. If you need a boolean-ish direction, coerce with `Math.sign(x) || 0` to collapse -0 to 0.

Related

← All JS Math methods · Array methods