Home >>Javascript Programs >How to find absolute value of a number in JavaScript

How to find absolute value of a number in JavaScript

How to find absolute value of a number in JavaScript?

JavaScript provide the way to find the absolute value of a number. Absolute value only represents the number not the sign of positive or negative means, it only displays the number suppose the absolute value of -5 is 5. JavaScript provides a function Math.abs() function which returns the absolute value of the number passed to it. In this article we will discuss how to find absolute value of a number in JavaScript.

Let's take an example:

<!DOCTYPE html>
<html>
<body>
<button onclick="myAbsolute()">Try it</button>
<p id="demo"></p>
<script>
function myAbsolute() {
  var a = Math.abs(10.25);
  var b = Math.abs(-10.25);
  var c = Math.abs("Hello");
  var d = Math.abs(7+5);

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

 


No Sidebar ads