There is the possibility of integer overflow when you try to compare numbers using subtraction in Java. See Q: Java Integer compareTo() - why use comparison vs. subtraction?
But, unlike Java, JavaScript handles overflows and underflows.
The question is Does the following implementation of compareNumeric()
work correctly in JavaScript?
// compareNumeric() one-liner
function compareNumeric(a, b) {
return a - b;
}
EDIT. The compareNumeric
function should return
- negative value when a < b;
- 0 when a = b;
- positive value when a > b.
a
and b
are Numbers.