Math.abs()
Math.abs() 静态方法返回数字的绝对值。
¥The Math.abs() static method returns the absolute value of a number.
Try it
语法
参数
返回值
描述
示例
使用 Math.abs()
参数强制
¥Coercion of parameter
Math.abs() 将其参数强制为数字.不可强制值将变为 NaN,使 Math.abs() 也返回 NaN。
¥Math.abs() coerces its parameter to a number. Non-coercible values will become NaN, making Math.abs() also return NaN.
js
Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(""); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1, 2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-math.abs |
浏览器兼容性
BCD tables only load in the browser