Javascript examples for Math:abs
The abs() method returns the absolute value of a number.
Parameter | Description |
---|---|
x | Required. A number |
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:
<!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>