English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Math.abs()The method returns the absolute value of a number.
Because abs() is a static method of Math, you always use it asMath.abs(), rather than as a method of the created Math object.
Math.abs(x)
Math.abs(-5);Test and see‹/›
All browsers fully support the Math.abs() method:
Method | |||||
Math.abs() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
x | A numeric value |
Return value: | Absolute value of a given number |
---|---|
JavaScript Version: | ECMAScript 1 |
Passing an empty object, an array with multiple members, a non-numeric string, or undefined/An empty variable will return NaN.
Passing null, an empty string, or an empty array will return 0.
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'); // NaNTest and see‹/›