[javascript] How to check if a number is between two values?

In JavaScript, I'm telling the browser to do something if the window size is greater than 500px. I do it like so:

if (windowsize > 500) {
    // do this
}

This works great, but I would like to apply this same method, but with a range of numbers. So I would like to tell my browser to do stuff if the window size is between 500px and 600px. I know this wouldn't work, but here is how I imagined it:

if (windowsize > 500-600) {
    // do this
}

Is this even possible, within JavaScript?

This question is related to javascript

The answer is


I had a moment, so, although you've already accepted an answer, I thought I'd contribute the following:

_x000D_
_x000D_
Number.prototype.between = function(a, b) {_x000D_
  var min = Math.min.apply(Math, [a, b]),_x000D_
    max = Math.max.apply(Math, [a, b]);_x000D_
  return this > min && this < max;_x000D_
};_x000D_
_x000D_
var windowSize = 550;_x000D_
_x000D_
console.log(windowSize.between(500, 600));
_x000D_
_x000D_
_x000D_

JS Fiddle demo.

Or, if you'd prefer to have the option to check a number is in the defined range including the end-points:

_x000D_
_x000D_
Number.prototype.between = function(a, b, inclusive) {_x000D_
  var min = Math.min.apply(Math, [a, b]),_x000D_
    max = Math.max.apply(Math, [a, b]);_x000D_
  return inclusive ? this >= min && this <= max : this > min && this < max;_x000D_
};_x000D_
_x000D_
var windowSize = 500;_x000D_
_x000D_
console.log(windowSize.between(500, 603, true));
_x000D_
_x000D_
_x000D_

JS Fiddle demo.

Edited to add a minor amendment to the above, given that – as noted in the comments –

Function.prototype.apply() is slow! Besides calling it when you have a fixed amount of arguments is pointless…

it was worth removing the use of Function.prototype.apply(), which yields the amended versions of the above methods, firstly without the 'inclusive' option:

_x000D_
_x000D_
Number.prototype.between = function(a, b) {_x000D_
  var min = Math.min(a, b),_x000D_
    max = Math.max(a, b);_x000D_
_x000D_
  return this > min && this < max;_x000D_
};_x000D_
_x000D_
var windowSize = 550;_x000D_
_x000D_
console.log(windowSize.between(500, 600));
_x000D_
_x000D_
_x000D_

JS Fiddle demo.

And with the 'inclusive' option:

_x000D_
_x000D_
Number.prototype.between = function(a, b, inclusive) {_x000D_
  var min = Math.min(a, b),_x000D_
    max = Math.max(a, b);_x000D_
_x000D_
  return inclusive ? this >= min && this <= max : this > min && this < max;_x000D_
}_x000D_
_x000D_
var windowSize = 500;_x000D_
_x000D_
console.log(windowSize.between(500, 603, true));
_x000D_
_x000D_
_x000D_

JS Fiddle demo.

References:


Here is the shortest method possible:

if (Math.abs(v-550)<50) console.log('short')
if ((v-500)*(v-600)<0) console.log('short')

Parametrized:

if (Math.abs(v-max+v-min)<max+min) console.log('short')
if ((v-min)*(v-max)<0) console.log('short')

You can divide both sides by 2 if you don't understand how the first one works;)


You can use Multiple clause in if condition instead of writing

if (windowsize > 500-600) {
    // do this
}

because this really makes no sense logically JavaScript will read your if condition like

windowSize > -100 

because it calculates 500-600 to -100

You should use && for strict checking both cases for example which will look like this

if( windowSize > 500 && windowSize < 600 ){

// Then doo something

}

It's an old question, however might be useful for someone like me.

lodash has _.inRange() function https://lodash.com/docs/4.17.4#inRange

Example:

_.inRange(3, 2, 4);
// => true

Please note that this method utilizes the Lodash utility library, and requires access to an installed version of Lodash.


this is a generic method, you can use everywhere

const isBetween = (num1,num2,value) => value > num1 && value < num2 

I just implemented this bit of jQuery to show and hide bootstrap modal values. Different fields are displayed based on the value range of a users textbox entry.

$(document).ready(function () {
    jQuery.noConflict();
    var Ammount = document.getElementById('Ammount');

    $("#addtocart").click(function () {

            if ($(Ammount).val() >= 250 && $(Ammount).val() <= 499) {
                {
                    $('#myModal').modal();
                    $("#myModalLabelbronze").show();
                    $("#myModalLabelsilver").hide();
                    $("#myModalLabelgold").hide();
                    $("#myModalPbronze").show();
                    $("#myModalPSilver").hide();
                    $("#myModalPGold").hide();
                }
            }
    });

I prefer to put the variable on the inside to give an extra hint that the code is validating my variable is between a range values

if (500 < size && size < 600) { doStuff(); }