1

(first stackoverflow post) This is a javaScript question, but I think the logic applies anywhere

I've wondered for a long time about using an if statement to compare multiple values against the same variable without needing to type out the variable every time; specifically, in regards to "greater than" and "less than".

The specific example I'm trying to solve is:

var middle = 5;
if(middle < 10 && middle > 0) {
    //some stuff
}

I'm looking for something like:

var middle = 5;
if(middle (< 10 && > 0)) {
    //the same stuff
}

I have checked is-there-a-way-to-shorten-the-condition-if-it-always-compares-to-the-same-thing and if-statements-matching-multiple-values and if-statement-multiple-conditions-same-statement. I could not derive a simple answer to my specific question from those.

A final note: I do not want to make a new "multiple if" function that accepts arguments and then compares them for the variable. I am looking for a "built-in" way to do this.

Thanks, -Charles

Community
  • 1
  • 1
goodguy5
  • 13
  • 1
  • 4
  • how many range binding comparisons do you actually need? Usually only two are necessary. – zzzzBov Mar 27 '14 at 21:10
  • 4
    There is no such way. You can write a wrapper function that returns a boolean value, but you'll still have to write the logic out in that. – Etheryte Mar 27 '14 at 21:11
  • Huh... After some (failed) attempts, I've determined that it's not as easy as I thought to make the wrapper function without using eval(). Any tips on this side question? – goodguy5 Mar 28 '14 at 15:47

5 Answers5

4

Some languages like Python and Coffeescript (which compiles to JS!) have chained comparisons:

if (0 < middle < 10)

However, Javascript does not have these. Just as you already do, you will need to reference the variable twice:

if (0 < middle && middle < 10)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

I don't believe it's possible, for a logical operator you need both sides of the operator to evaluate to true or false.

If you are missing a side the statement can't be evaluated.

MrHaze
  • 3,786
  • 3
  • 26
  • 47
0

I am looking for a "built-in" way to do this.

Javascript doesn' t support this feature. In fact, most languages don't and Python is the only famous example I can think of right now that does that (you can write 0 < middle < 10 there).

In Javascript, the best you can do is use a shorter variable name

if(0 <= x && x < 10)

Or abstract common tests into a function

if(between(0, x, 10))
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
hugomg
  • 68,213
  • 24
  • 160
  • 246
0

Right, don't do this, but just for the sake of it :

if (Math.min(10, Math.max(0, middle)) == middle) {
   // middle is between 0 and 10
}
Theo.T
  • 8,905
  • 3
  • 24
  • 38
0

If anyone is doing this in python for multiple values you can try this

exclude = ['10','0','20','30','12','14']
if i not in exclude:
   print(i)
Ankit Arora
  • 87
  • 10
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 20 '21 at 04:35