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”

function difference(n, m){
return Math.abs(n - m)
}

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

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.

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

6.9ms 11.7 7.0 4.4

7.5ms 4.4 6.4 4.5

7.8ms 5.1 6.2 6.8

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 ? ) .

Here is a simple function

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

It means you want to return absolute value.

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.

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

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:

vs

(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.

In

`TypeScript`

, if anyone interested:defining four functions I made a little benchmark, surprisingly

Here are the results for 1million iterations:

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

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 ? ) .

real casino slots https://onlinecasinosure.com/# – play slots online play online casino slot games

royal river casino vegas world free games online best casino slots online zone online casino slots

medterra cbd http://cbdforsalesh.com/# – cbd oil benefits buy cbd oil buy cbd oil online

dating naked https://tinderdatingww.com/# – dating sites free zoosk online dating site speed dating