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.
Use Math. abs() inside a JavaScript function to get the difference between two numbers in JavaScript.
How to Find the Difference between Two Numbers. To find the difference between two numbers, subtract the number with the smallest value from the number with the largest value. The product of this sum is the difference between the two numbers.
The Math. abs() function returns the absolute value of a number. That is, it returns x if x is positive or zero, and the negation of x if x is negative.
By using abs() function we can get the difference of two integer numbers without comparing them, abs() is a library function which is declared in stdlib. h – This function returns the absolute value of given integer.
var difference = function (a, b) { return Math.abs(a - b); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With