How to convert a negative number to a positive one in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To convert a negative number to a positive one in JavaScript, use the abs() method in JavaScript. The method returns the absolute value of a number.

Example

You can try to run the following code to convert a negative number to positive −

<html>
   <head>
      <title>JavaScript Math abs() Method</title>
   </head>
   <body>
      <script>
         var value = Math.abs(-1);
         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 );

         var value = Math.abs("string");
         document.write("<br />Fourth Test Value : " + value );
      </script>
   </body>
</html>
raja
Published on 16-Feb-2018 12:15:34
Advertisements