How to check if a number is between two values?

In JavaScript, I tell the browser to do something if the window is larger than 500 pixels. I do it like this:

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

This works great, but I would like to apply the same method, but with a range of numbers. Therefore, I would like my browser to do something if the window size is between 500 and 600 pixels. I know this will not work, but here is how I imagined it:

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

Is this even possible in JavaScript?

+121
javascript
Feb 05 '13 at 22:56
source share
5 answers

Checks whether windowsize greater than 500 and less than 600 , which means that neither 500 nor 600 by themselves will cause the condition to become true.

 if (windowsize > 500 && windowsize < 600) { // ... } 
+197
Feb 05 '13 at 22:58
source share
β€” -

I had a moment, therefore, although you already accepted the answer, I thought that I would introduce the following:

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

JS Fiddle demo .

Or, if you prefer the option of checking the number to be in a certain range, including endpoints:

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

JS Fiddle demo .

Edited to add a slight correction to the above, given that - as noted in the comments:

& hellip; Function.prototype.apply() slow! Also, to call it when you have a fixed number of arguments is pointless & hellip;

it was advisable to remove the use of Function.prototype.apply() , which gives the corrected versions of the above methods, firstly, without the "inclusive" option:

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

JS Fiddle demo .

And with the option "inclusive":

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

JS Fiddle demo .

Literature:

+103
Sep 18 '13 at 20:31 on
source share

I prefer to put the variable inside to give an additional hint that the code is checking my variable is between the range values

 if (500 < size && size < 600) { doStuff(); } 
+68
Feb 05 '13 at 23:06
source share

This is an old question, but it can be useful for people like me.

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

Example:

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

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

+20
May 25 '17 at 15:12
source share

I just implemented this jQuery bit to show and hide load modality values. Different fields are displayed based on a range of values ​​for a user text field 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(); } } }); 
0
Sep 18 '13 at 20:22
source share



All Articles