I have a simple 2 line procedure that's like
if ( bgwidth < 0 ) { bgwidth = 0; }
else if ( bgwidth > 100 ) { bgwidth = 100; }
and I'm wondering what is the most compact way to do it. Is there some way with bitshift operators? :)
I have a simple 2 line procedure that's like
if ( bgwidth < 0 ) { bgwidth = 0; }
else if ( bgwidth > 100 ) { bgwidth = 100; }
and I'm wondering what is the most compact way to do it. Is there some way with bitshift operators? :)
Math.min()
and Math.max()
might do the trick:
Math.min(Math.max(0, bgwidth), 100);
And for readbility:
function fitBetween(value, min, max) {
return Math.min(Math.max(min, value), max);
}