The Math.sign()
static method returns 1 or -1, indicating the sign of the number passed as argument. If the input is 0 or -0, it will be returned as-is.
The Math.sign()
static method returns 1 or -1, indicating the sign of the number passed as argument. If the input is 0 or -0, it will be returned as-is.
Math.sign(x)
x
A number.
A number representing the sign of x
:
x
is positive, returns 1
.x
is negative, returns -1
.x
is positive zero, returns 0
.x
is negative zero, returns -0
.NaN
.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).
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
Specification |
---|
ECMAScript Language Specification # sec-math.sign |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
sign |
38 | 12 | 25 | 25 | 9 | 38 | 25 | 25 | 9 | 3.0 | 38 | 1.0 | 0.12.0 |
© 2005–2023 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