--- title: Math.sign() slug: Web/JavaScript/Referencia/Objectes_globals/Math/sign translation_of: Web/JavaScript/Reference/Global_Objects/Math/sign ---
La funció Math.sign()
retorna el signe d'un nombre, indicant si el nombre donat és positiu, negatiu o zero.
Math.sign(x)
x
Com que sign()
és un mètode estàtic de Math
, sempre s'utilitza com Math.sign()
en comptes de com un mètode d'un objecte Math
creat (Math
no és un constructor).
Aquesta funció pot retornar 5 valors diferents, 1
, -1
, 0
, -0
, NaN
, que representen "nombre positiu", "nombre negatiu", "zero positiu", "zero negatiu" i {{jsxref("NaN")}} respectivament.
L'argument passat a aquesta funció serà convertit al tipus de x
implícitament.
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
Math.sign = Math.sign || function(x) { x = +x; // converteix a un nombre if (x === 0 || isNaN(x)) { return x; } return x > 0 ? 1 : -1; }
Especificació | Estat | Comentaris |
---|---|---|
{{SpecName('ES6', '#sec-math.sign', 'Math.sign')}} | {{Spec2('ES6')}} | Definició inicial. |
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | {{CompatChrome("38")}} | {{CompatGeckoDesktop("25")}} | {{CompatNo}} | {{CompatOpera("25")}} | {{CompatNo}} |
Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoMobile("25")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |