`Math.abs(x)` 函数返回指定数字 “x“ 的绝对值。如下：

Math.abs(x)=|x|={x if x≥0-x if x<0{\mathtt{\operatorname{Math.abs}(x)}} = {|x|} = \begin{cases} x & \text{if} \quad x \geq 0 \\ -x & \text{if} \quad x < 0 \end{cases}

## 语法

    Math.abs(x);

### 参数

`x`
  一个数值

## 说明

`由于 Math.abs()` `是` `Math` 中的一个静态方法，你应该通过 `Math.abs()` `调用。`（Math 不是构造器）

## 示例

### `Math.abs()函数的行为`

传入一个非数字形式的字符串或者 undefined/empty 变量，将返回 `NaN`。传入 null 将返回 0。

    Math.abs('-1');     // 1
    Math.abs(-2);       // 2
    Math.abs(null);     // 0
    Math.abs("string"); // NaN
    Math.abs();         // NaN

## 规范

规范版本 | 规范状态 | 注解
--- | --- | ---
ECMAScript 1st Edition. Implemented in JavaScript 1.0 | Standard | Initial definition.
ECMAScript 5.1 (ECMA-262) Math.abs | Standard |
ECMAScript 2015 (6th Edition, ECMA-262) Math.abs | Standard |

## 浏览器兼容性

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!

- Desktop
- Mobile

Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari
--- | --- | --- | --- | --- | ---
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes)

Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile
--- | --- | --- | --- | --- | --- | ---
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes)

## 相关链接

- The `Math` object it belongs to.
- `Math.ceil()`
- `Math.floor()`
- `Math.round()`