Math.sign()

Returns the sign of a number, indicating whether it is positive, negative, or zero.

Since ES2015 Spec ↗

Syntax

Math.sign(x)

Parameters

NameTypeRequiredDescription
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

EnvironmentSince version
chrome 38
firefox 25
safari 9
edge 12
node 0.12

See also