Trying to compare 2 integers' values, X and Y. If X is greater than Y, I want to cap off X at Y, like this -
if(x>y) {
x=y;
}
Is there a shorthand way to do this (either in pure JavaScript or jQuery)? I was thinking of using a custom function, but wanted to see if something already existed.
Thanks!
You can use Math.min():
x = Math.min(x, y);
The only I can think of:
x>y && (x=y);
And fastest in Chrome 22 (thought if(...)
would be faster): http://jsperf.com/if-min
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