English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Math.abs() Method

 JavaScript Math Object

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.

Syntax:

Math.abs(x)
Math.abs(-5);
Test and see‹/›

Browser Compatibility

All browsers fully support the Math.abs() method:

Method
Math.abs()IsIsIsIsIs

Parameter Value

ParameterDescription
xA numeric value

Technical Details

Return value:Absolute value of a given number
JavaScript Version:ECMAScript 1

More Examples

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'); // NaN
Test and see‹/›

 JavaScript Math Object