Math abs() Method - Javascript Math

Javascript examples for Math:abs

Description

The abs() method returns the absolute value of a number.

Parameter Values

Parameter Description
x Required. A number

Return Value:

A Number, representing the absolute value of the specified number, or NaN if the value is not a number, or 0 if the value is null

The following code shows how to Return the absolute value of a number:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from w  w w  . j  a  v a2  s .  c  o  m
    var a = Math.abs(17.25);
    var b = Math.abs(-17.25);
    var c = Math.abs(null);
    var d = Math.abs("Hello");
    var e = Math.abs(21+31);

    var x = a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials