JavaScript tutorial:
abs method

 

Applies to: Math Object

The abs method is use to get the absolute value of a number.

Syntax

Math.abs(number)

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

Return value

Returns the absolute value of a number.

Example

The return value is the absolute value of the number argument.

The following example illustrates the use of the abs method:

function ComparePosNegVal(n)
{
var s;
var v1 = Math.abs(n);
var v2 = Math.abs(-n);
if (v1 = v2)
s = "The absolute values of " + n + " and "
s += -n + " are identical.";
return(s);
}
document.write(ComparePosNegVal(3.69));

To run the code above, paste it into JavaScript Editor, and click the Execute button

See also: Math Object Methods