I want to be able to test whether a value is within a number range. This is my jQuery code...
if ((year < 2099) && (year > 1990)){
return 'good stuff';
}
Is there a simpler way to do this in jQuery? For example, is there something like this...
if (1990 < year < 2099){
return 'good stuff';
}
From a similar solution here: http://indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is-between-two-numbers/
If you don't like the boolean operator, you could always use nested if statements:
You could make your own method:
hope this helps.
Max
If you are asking which language has this feature, python does:
In many languages, the second way will be evaluated from left to right incorrectly with regard to what you want.
In C, for instance,
1990 < year
will evaluate to 0 or 1, which then becomes1 < 2099
, which is always true, of course.Javascript is a quite similar to C:
1990 < year
returnstrue
orfalse
, and those boolean expressions seem to numerically compare equal to 0 and 1 respectively.But in C#, it won't even compile, giving you the error:
You get a similar error from Ruby, while Haskell tells you that you cannot use
<
twice in the same infix expression.Off the top of my head, Python is the only language that I'm sure handles the "between" setup that way:
The bottom line is that the first way
(x < y && y < z)
is always your safest bet.The fast and simple way to make this is to create a function like this:
Then use that as follows:
Or like this: