Math.abs(x) 函数返回指定数字 “x“ 的绝对值。如下:
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 |
| 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) |
Math object it belongs to.Math.ceil()Math.floor()Math.round()