Javascript function to get the difference between two numbers

I want a Simple Javascript function to get the difference between two numbers in such a way that foo(2, 3) and foo(3,2) will return the same difference 1.

12 thoughts on “Javascript function to get the difference between two numbers”

  1. Here is a simple function

    function diff (num1, num2) {
      if (num1 > num2) {
        return num1 - num2
      } else {
        return num2 - num1
      }
    }
    

    And as a shorter, one-line, single-argument, ternary-using arrow function

    function diff (a, b) => a > b ? a - b : b - a
    
    Reply
  2. I don’t get how this is possible on such a simingly common question, but all the answers I found here are wrong in certain cases.

    If both numbers are on the same side of zero then the accepted answer is right, but if the numbers are not on the same side of zero, then their absolute values must be added, not subtracted.

    function diff( x, y ) {
    
        if ( Math.sign( x ) === Math.sign( y ) ) {
    
            return Math.abs( x - y );
    
        } else {
    
            return Math.abs( x ) + Math.abs( y );
    
        };
    
    };
    
    diff( 2, 2 ) // 0
    diff( -2, -2 ) // 0
    diff( -2, 2 ) // 4
    diff( 2, -2 ) // 4
    

    Solution on math.stackexchange.com : https://math.stackexchange.com/a/1893992

    Reply
  3. Seems odd to define a whole new function just to not have to put a minus sign instead of a comma when you call it:

    Math.abs(a - b);
    

    vs

    difference(a, b);
    

    (with difference calling another function you defined to call that returns the output of the first code example). I’d just use the built in abs method on the Math object.

    Reply
  4. In TypeScript, if anyone interested:

    public getDiff(value: number, oldValue: number) {
        return value > oldValue ? value - oldValue : oldValue - value;
    }
    
    Reply
  5. defining four functions I made a little benchmark, surprisingly

    fn1 = (a,b)=>(a>b)?a-b:b-a;
    fn2 = (a,b)=>{let c= a-b;return c>0?c:-c;}
    fn3 = (a,b)=>Math.abs(a-b)
    fn4 = (a,b)=>Math.max(a-b, b-a)
    
    
    for(let fn of [ fn1, fn2, fn3, fn4])
     executionTime(()=>{console.log(fn);for(let i=0;i<1_000_000;i++)fn(500_000,i)})
     
    
     
     
     
     function executionTime(cb){//calculate performance
        const t0 = performance.now();
        cb();
        const t1 = performance.now();
        console.log(`cb took ${t1 - t0} milliseconds.`);
    }

    Here are the results for 1million iterations:

    1. cb took 11.129999998956919 milliseconds.
    2. cb took 53.420000011101365 milliseconds.
    3. cb took 47.40000003948808 milliseconds.
    4. cb took 48.20000007748604 milliseconds.

    But unfortunalelly those are false results, I mean executing one by one gave on the console resulting:

    1. 6.9ms 11.7 7.0 4.4
    2. 7.5ms 4.4 6.4 4.5
    3. 7.8ms 5.1 6.2 6.8
    4. 3.9ms 6.3 5.2 5.2

    I think while looping, after each iteration garbage is collecting while other functions doing their bests. While executing each one by one, fn2 came second, while using extra variable( vm is optimizing with register ? ) .

    Reply

Leave a Comment