Computer >> Computer tutorials >  >> Programming >> Javascript

How to get the absolute value of a number in JavaScript?


To get the absolute value of a number in JavaScript, use the Math.abs() function. It returns the absolute value. The following is the parameter supported by the abs() method −

  • x − A number

Example

You can try to run the following code to learn how to get the absolute value of a number −

<html>
   <head>
      <title>JavaScript Math abs() Method</title>
   </head>
   
   <body>
      <script>
         var value = Math.abs(-10);
         document.write("First Test Value : " + value );
         var value = Math.abs(null);
         document.write("<br />Second Test Value : " + value );
         var value = Math.abs(20);
         document.write("<br />Third Test Value : " + value );
      </script>
   </body>
</html>