Math.sign()
The Math.sign()
function returns either a
positive or negative +/- 1, indicating the sign of a
number passed into the argument. If the number passed into Math.sign()
is
0, it will return a +/- 0. Note that if the number is positive, an explicit (+) will
not be returned.
Syntax
Math.sign(x)
Parameters
x
-
A number. If this argument is not a
number
, it is implicitly converted to one.
Return value
A number representing the sign of the given argument:
- If the argument is positive, returns
1
. - If the argument is negative, returns
-1
. - If the argument is positive zero, returns
0
. - If the argument is negative zero, returns
-0
. - Otherwise,
NaN
is returned.
Description
Because sign()
is a static method of Math
, you always use it
as Math.sign()
, rather than as a method of a Math
object you
created (Math
is not a constructor).
Examples
Using Math.sign()
Math.sign(3); // 1
Math.sign(-3); // -1
Math.sign('-3'); // -1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
Math.sign('foo'); // NaN
Math.sign(); // NaN
Specifications
Specification |
---|
ECMAScript Language Specification # sec-math.sign |
Browser compatibility
BCD tables only load in the browser