javascript - Shortening Multiple IF Conditions for the Same Variable - Stack Overflow

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

(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 pare 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-pares-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 pares them for the variable. I am looking for a "built-in" way to do this.

Thanks, -Charles

(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 pare 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-pares-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 pares them for the variable. I am looking for a "built-in" way to do this.

Thanks, -Charles

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Mar 27, 2014 at 21:08 goodguy5goodguy5 131 silver badge4 bronze badges 3
  • how many range binding parisons do you actually need? Usually only two are necessary. – zzzzBov Commented Mar 27, 2014 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 Commented Mar 27, 2014 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 Commented Mar 28, 2014 at 15:47
Add a ment  | 

5 Answers 5

Reset to default 4

Some languages like Python and Coffeescript (which piles to JS!) have chained parisons:

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)

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.

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
}

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 mon tests into a function

if(between(0, x, 10))

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745668156a4639242.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信