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

How to write a JavaScript function to get the difference between two numbers?


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>