Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 577
Mithun Sreedharan Avatar asked Jul 01 '10 10:07

Mithun Sreedharan


People also ask

How do I get the difference between two numbers in JavaScript?

Use Math. abs() inside a JavaScript function to get the difference between two numbers in JavaScript.

How do you find the difference in between two numbers?

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.

How do you find the absolute difference in JavaScript?

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.

Which function returns the difference of two integers?

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.


1 Answers

var difference = function (a, b) { return Math.abs(a - b); } 
like image 121
mykhal Avatar answered Oct 25 '22 21:10

mykhal