Math.sign()
Returns the sign of a number, indicating whether it is positive, negative, or zero.
Syntax
Math.sign(x) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
x | number | Yes | The number to evaluate. |
Returns
number — 1, -1, 0, -0, or NaN depending on the sign of x.
Examples
console.log(Math.sign(3), Math.sign(-3), Math.sign(0));
Output
1 -1 0
console.log(Math.sign(-0), Math.sign(NaN));
Output
-0 NaN
Notes
Returns five possible values: 1, -1, 0, -0, or NaN. Useful for branching on
direction without comparing magnitudes.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 38 |
| firefox | 25 |
| safari | 9 |
| edge | 12 |
| node | 0.12 |