I wanted to share this dead-simple yet very useful JavaScript snippet I created, with which you can make sure an integer stays within a certain minimum and maximum value. It takes three values (all integers): integer to check, minimum and maximum, and returns the integer to check, corrected to the limits if needed (when it falls out of min/max range).

## JavaScript code

```
function valBetween(v, min, max) {
return (Math.min(max, Math.max(min, v)));
}
```

Click here to try the code in a Fiddle.

View/fork on Github: https://gist.github.com/c-kick/1dfcdc91334a7de3a7af

## Examples

`valBetween(800, 0, 1000);`

Would output `800`

`valBetween(80, 0, 20);`

Would output `20`

`valBetween(-180, -100, -20);`

Would output `-100`

PS: Function can easily be translated to PHP:

## PHP code

```
function valBetween($val, $min, $max) {
return (min($max, max($min, $val)));
}
```

Update dec 3 2014: As Maciej points out in the comments, this is an alternate, potentially faster method (see tests at http://jsperf.com/between-min-max). Though the tests do not give a clear winner, as the results vary greatly on each run.

I was primarily going for super simple code, but I’ll provide Maciej’s code as an alternative:

```
function valBetweenAlt(v, min, max) {
if (val > min) {
if (val < max) {
return val;
} else return max;
} else return min;
}
```

And minified:

```
function valBetweenAltMin(v, min, max) {
return (val > min) ? ((val < max) ? val : max) : min;
}
```

Micro Optimizations like those make no sense at all. 1. This can change in every browser version. 2. The applications bottleneck will never ever be this function… ever.

Also function names should (be / starting with) verbs. With a name like valBetween I would be tempted to write:

if (valBetween(1, 0, 2)) { … }

A common name for this function would be “clamp”.

More readable would be:

Math.min(Math.max(min, v), max)

because min, v, max are in order.

Really useful, took a while to get my head arund such a simple thing but makes perfect sense. Inner outputs largest, outer compares against max and returns the smallest if we are out of bounds!

[…] Note: valBetween is a clever little one-liner I found on l’internets. […]

this function

function maxmin(val, min, max) {

if (val > min) {

if (val < max) {

return val;

} else return max;

} else return min

}

is a lot quicker and can be even more minified, so it would be only a little bit longer.

Interesting! I did some tests, but the results are not constant (in Chrome). Sometimes my code is faster, and sometimes yours. See the test: http://jsperf.com/limit-max-min-tests

I was going for super-simplicity on this one, but I’ve added your suggestion, see the edit!

A suggestion for minifying your example even more (besides making it a single line function) would be this:

function maxmin(val, min, max) {

return (val > min) ? ((val < max) ? val : max) : min; } But unfortunately, those ternaries seem to slow the function down (see tests) a lot. But then again, I'm no JS expert ðŸ™‚

Sorry. please remove duplicated comment it should be in reply.

Donâ€™t create functions in a loop (unless you will have to prototype them in every iteration thereâ€™s no need for that).

http://jsperf.com/between-min-max

Oops, you’re right. I’ll update the link. Though the tests still don’t show a clear winner, the differences vary on each run, on each browser, somehow.