Math.abs Function (JavaScript)

Returns the absolute value of a number (the value without regard to whether it is positive or negative). For example, the absolute value of -5 is the same as the absolute value of 5.




The required number argument is a numeric expression for which the absolute value is needed.

Return Value

The absolute value of the number argument.


The following example illustrates the use of the abs function.

var s;  
var v1 = Math.abs(6);  
var v2 = Math.abs(-6);  
if (v1 == v2) {  
    document.write("Absolute values are the same.");  
else {  
document.write("Absolute values are different.");  

// Output: Absolute values are the same.  


Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Applies To: Math Object

See Also

Math Object