JavaScript - Math.abs() Method
The JavaScript Math.abs() method returns the absolute value (positive value) of the specified number. For example - absolute value of x will be |x|.
Syntax
Math.abs(x)
Parameters
x |
Specify a number whose absolute value need to be determined. |
Return Value
Returns the absolute value (positive value) of the argument.
Example:
In the example below, Math.abs() method returns the absolute value (positive value) of the specified number.
var txt; txt = "Math.abs(10) = " + Math.abs(10) + "<br>"; txt = txt + "Math.abs(-10) = " + Math.abs(-10) + "<br>"; txt = txt + "Math.abs(1.5) = " + Math.abs(1.5) + "<br>"; txt = txt + "Math.abs(-1.5) = " + Math.abs(-1.5) + "<br>";
The output (value of txt) after running above script will be:
Math.abs(10) = 10 Math.abs(-10) = 10 Math.abs(1.5) = 1.5 Math.abs(-1.5) = 1.5
❮ JavaScript - Math Object