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.

45 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

finasteride 1mg – http://finasteridepls.com/ hair loss regimen without propecia

propecia results reddit – generic propecia price propecia before and after reddit

tadalafil generic – thuб»‘c tadalafil stada 10mg generic tadalafil reviews

Ecyvua – ivermectin uk coronavirus Yseidv sztuot

Vlwulc – best over the counter erectile dysfunction pills Twncvn swnqvr

Ycbkbl – lasix 20 mg Csqwuz rnlveu

Fxmptr – cialis 5 mg tablets cost Qptxxf wciiyn

Ylydyy – buy generic sildenafil online Bzydyt hhwlvh

Qqqnat – affordable essay writing Pojapi vzfton

Dwomdw – cheap sildenafil tablets 100mg Chlskr khvnql

Qlzxzq – research paper juvenile delinquency Qqbsrn yphikk

Hlaxok – article journal sur le viagra Yimnzr xiwsjg

Adhiyg – buy topical finasteride usa Ewhaki hsdiwa

Chtnls – essay writing packet Cwyhug uzfyth

Ircicw – sildenafil 100mg goodrx Nwsagj rlpfud

Akbzmm – ace homework Rlycbi yzsrdj

Izuldt – furosemide 40mg Wzgyel vttzev

Dwjmkb – vardenafil free sample Dvmras zgbfdu

Sxwqhx – silagra 25 mg Gqfhof ugqzie

Ulxatz – silagra tablets Lmkwsq zguodl

Wevvjj – tadacip best price Qfcplb jyfpsf

tadacip price – buy tadacip canada tadacip 20 for sale

celebrex medication cost – celebrex 200 mg price in india where can you get celebrex

stromectol tablets 3 mg – ivermectin cost in usa ivermectin tablet 1mg

yasmin 21 tablets price – nexium 22 mg nexium generic 40mg

prednisone 50 tablet – prednisone 20mg capsule prednisone otc canada

how to buy viagra online in australia – canadian viagra professional sildenafil cheap pills

can i buy viagra over the counter australia – viagra cialis viagra by phone

lyrica 10 mg – lyrica price in india lyrica medicine

nexium 40 mg canada – topamax generic price zofran daily

ivermectin tablets – price of ivermectin liquid ivermectin cost canada

ivermectin 3mg price – where to buy stromectol online ivermectin goodrx

cheap zofran – zofranpl.com cheap ondansetron 8 mg