metadata
title: Math.sign()
short-title: sign()
slug: Web/JavaScript/Reference/Global_Objects/Math/sign
page-type: javascript-static-method
browser-compat: javascript.builtins.Math.sign
sidebar: jsref
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.
{{InteractiveExample("JavaScript Demo: Math.sign()")}}
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 {{jsxref("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
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- Polyfill of
Math.signincore-js - es-shims polyfill of
Math.sign - {{jsxref("Math.abs()")}}
- {{jsxref("Math.ceil()")}}
- {{jsxref("Math.floor()")}}
- {{jsxref("Math.round()")}}
- {{jsxref("Math.trunc()")}}