# Math.sign()

`Math.sign()` 函数返回一个数字的符号, 指示数字是正数，负数还是零。

## 语法

``Math.sign(x);``

`x`

## 示例

### 使用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
``````

## Polyfill

``````function sign(x) {
x = +x ;// convert to a number
if (x === 0 || isNaN(x))
return x;
return x > 0 ? 1 : -1;
}``````

``````if (!Math.sign) {
Math.sign = function(x) {
// If x is NaN, the result is NaN.
// If x is -0, the result is -0.
// If x is +0, the result is +0.
// If x is negative and not -0, the result is -1.
// If x is positive and not +0, the result is +1.
x = +x; // convert to a number
if (x === 0 || isNaN(x)) {
return Number(x);
}
return x > 0 ? 1 : -1;
};
}``````

## 规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Math.sign
Standard Initial definition.

## 浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help! (en-US)

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 38 25 (25) 未实现 25 未实现
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 未实现 未实现 25.0 (25) 未实现 未实现 未实现