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.
Math.sign()
Try it
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
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
sign |
38
|
12
|
25
|
No
|
25
|
9
|
38
|
38
|
25
|
25
|
9
|
3.0
|
1.0
|
0.12.0
|
See also
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign