Get Difference Between Two Numbers Using JavaScript Function



Use Math.abs() inside a JavaScript function to get the difference between two numbers in JavaScript.

Example

You can try to run the following code to get the difference of numbers

Live Demo

<html>
   <head>
      <script>
         var num1, num2;
         num1 = 50;
         num2 = 35;
         var difference = function (num1, num2){
            return Math.abs(num1 - num2);
         }
         document.write("Difference = "+difference(num1,num2));
      </script>
   </head>
   <body></body>
</html>
Updated on: 2020-01-08T06:46:47+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements