Math.min and Math.max

You’ve probably heard of and used the Math.min and Math.max functions in JavaScript before, they’re pretty self explanatory.

Math.min(5, 10); //=> 5
Math.max(5, 10); //=> 10

Something useful

One trick that I found particularly handy, is when using both Math.min and Math.max together to constrain a number between two bounds.

var min = 5;
var max = 10;

Math.max(min, Math.min(15, max)); //=> 10
Math.max(min, Math.min(2, max)); //=> 5
Math.max(min, Math.min(7, max)); //=> 7

Something to take-away

We can use ‘currying’ to create a dynamic constraining function that will constrain numbers to the specified bounds.

function constrainer(min, max) {
  return function(n) {
    return Math.max(min, Math.min(n, max));
  };
}

var constrain = constrainer(5, 10);

constrain(15); //=> 10
constrain(2); //=> 5
constrain(7); //=> 7

Demo

comments powered by Disqus