Shorthand for if (x> y) {x = y} in javaScript

Trying to compare 2 values ​​of integers, X and Y. If X is greater than Y, I want to disable X in Y, like this -

if(x>y) { x=y; } 

Is there a shorthand way to do this (either in pure JavaScript or in jQuery)? I was thinking of using a custom function, but wanted to see if something already exists.

Thanks!

+4
source share
3 answers

You can use Math.min () :

 x = Math.min(x, y); 
+6
source

The only thing I can think of:

 x>y && (x=y); 

And the fastest in Chrome 22 (the thought of if(...) will be faster): http://jsperf.com/if-min

+2
source

Yes,

 x = Math.min(x,y); 

Welcome!

+1
source

Source: https://habr.com/ru/post/1442360/


All Articles