Home >>JavaScript Math Reference >JavaScript abs() Method

JavaScript abs() Method

JavaScript abs() Method

JavaScript abs() method is used to return the absolute value of a given input number. It takes only a number as its parameter and returns its absolute value as the result.

Syntax:

Math.abs(x)

Parameter Values

Parameter Description
x This is a required parameter. It defines the given input number.

Browser Support

Method Chrome Edge Firefox Safari Opera
abs() Yes Yes Yes Yes Yes

Here is an example of abs() method:

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = Math.abs(-55.22);
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

 

Example 2:

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() {
  var a = Math.abs(10.06);
  var b = Math.abs(-10.06);
  var c = Math.abs(null);
  var d = Math.abs("PHP");
  var e = Math.abs(10+6);
  var x = a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e;
  document.getElementById("demo1").innerHTML = x;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

 


No Sidebar ads