Math.sign()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It's been available across browsers since July 2015.
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.
Try it
console.log(Math.sign(3));
// Expected output: 1
console.log(Math.sign(-3));
// Expected output: -1
console.log(Math.sign(0));
// Expected output: 0
console.log(Math.sign("-3"));
// Expected output: -1
Syntax
Math.sign(x)
Parameters
-
x - A number.
Return value
A number representing the sign of x:
- If
xis positive, returns1. - If
xis negative, returns-1. - If
xis positive zero, returns0. - If
xis negative zero, returns-0. - Otherwise, returns
NaN.
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® 2027 Language Specification # sec-math.sign |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on iOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
sign |
38 |
12 |
25 |
25 |
9 |
38 |
25 |
25 |
9 |
3.0 |
38 |
9 |
1.0.0 |
1.0 |
0.12.0 |
See also
- Polyfill of
Math.signincore-js - es-shims polyfill of
Math.sign Math.abs()Math.ceil()Math.floor()Math.round()Math.trunc()
© 2005–2025 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