-2

For example, instead of:

if (ext === "_st" || ext ==="_mt" || ext === "_00" ){
   //do something
}

I would intuitively expect that the following also works:

if (ext === ("_st" || "_mt" || "_00" )){
    //do something
}

But that doesn't work. Is there a way to avoid repeating the same ("ext" in this case) variable for more compressed and efficient code?

UPDATE: I did a search before asking and none of the "duplicate" questions appeared in the first 10-15 suggestions. Instead, StackOverflow made WRONG suggestions for irrelevant -false duplicates.

Here is my suggestion: Instead of marking questions as "duplicate" why not merge them along with the answers in one single question with an intuitive title?

Thanks for the answers.

UPDATE #2: This is NOT a duplicate. The suggested questions have answers that only refer to OR optimization while my question is more general, it refers to "logic expressions", NOT specifically to OR. I just provided an OR example. I'm specifically asking whether there is a method to avoid repeating the main variable in a logical statement whether it is OR, AND, etc.

For example: given var1 == var2 == var3

if (ext === var1 && ext === var2 && ext === var3 ){
   //do something
}
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
dllb
  • 59
  • 1
  • 7
  • 2
    Use an array: `[ "_st", "_mt", "_0" ].includes( ext )`. – Shilly Jun 12 '19 at 10:13
  • [Switch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) maybe? – CodeF0x Jun 12 '19 at 10:13
  • I'm not sure what you're trying to achieve, but there is **in** operator. [Look MDN here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) – i100 Jun 12 '19 at 10:15
  • @Shilly, `.includes` is not supported in older browsers [(see here)](https://caniuse.com/#feat=array-includes) so a [polyfill](https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) might be needed. – Michel Jun 12 '19 at 10:17
  • @Michel `.includes()` is just `.indexOf() > x`, so if .includes() is not supported, indexOf() will do. – Shilly Jun 12 '19 at 10:18
  • do you want in only js or in jquery – Yogendra Jun 12 '19 at 10:36
  • @dlb you are right, people who are experts in JavaScript can only find those duplicates because they know the wording or terminology to use to find the question. Tagging them as duplicate is fine but down voting doesn't justify their acts at all. Also stackoverflow suggestions while asking questions should also be improved based on the tags a user asks the question. So yes I totally agree with you. – Nagaraju Jun 12 '19 at 10:56
  • I have updated the question once more. My question is not specific to OR logical cases, therefore it is not "duplicate". – dllb Jun 12 '19 at 11:04
  • @dllb well, there isn't a way. The dupes should be very clear - you can only emulate the logical operation by using arrays as well as other expressions and statements but ultimately, you cannot shorten a logical expression. – VLAZ Jun 12 '19 at 11:08
  • 1
    @dllb also, how would you expect to have `ext === "_st" && ext ==="_mt" && ext === "_00"` that returns anything but `false`? – VLAZ Jun 12 '19 at 11:09
  • 1
    If you wanted an equivalent for an `&&` condition, you could use `Array#every` instead of `includes`… There isn't a generic answer, there are only specific answers for specific cases. – deceze Jun 12 '19 at 11:09
  • 1
    And posting a duplicate in and of itself isn't bad at all. The other Q&As already contain the answer, so your question is solved that way. No need to complain about that. If you didn't find that before… well, that happens, no big deal. – deceze Jun 12 '19 at 11:11
  • @VLAZ instead of strings there could be variables in an && contrition. – dllb Jun 12 '19 at 11:20
  • @deceze, There are definitely generic solutions in all kinds of problems, including code, albeit not in every case. Thanks for the Array#every suggestion. – dllb Jun 12 '19 at 11:21
  • @dllb but there is no way you can have `var1 == var2` *and* `var1 == var3` at the same time. Your question asks for a way to remove the repetition of `var1`. What logical expression do you have in mind that uses ANDs with the same variable? – VLAZ Jun 12 '19 at 11:23
  • @VLAZ Except if `var2 == var3`… ;) – deceze Jun 12 '19 at 11:33
  • @deceze I guess you're right... – VLAZ Jun 12 '19 at 11:34
  • @deceze the op might or not be complaining. But there is a valid point, the op didn't find the suggested questions useful. Okay, lets leave it aside, someone marked it as duplicate that too is fine. But why the down votes, is marking duplicate not enough(I am not saying you downvoted it). May be the downvoters could have commented the reason for their downvotes to help improve the op so that next time he/she can ask a question without repeating the same mistake. That is the reason the op must have updated the question (or complained in your words) – Nagaraju Jun 12 '19 at 11:53
  • 1
    If you want to have both "ands" and "ors" (instead of just seeing if an array contains something) then you are writing a series of comparisons. I don't see how it could be possible to write a comparison without specifying both sides of the comparison. Also, if your question isn't a duplicate because in your "real" scenario it's not all "ors", why not just eliminate the confusion and make some some of the conditions in your question "ands". It's a lot clearer than making them all "ors" and then adding a paragraph to explain that's it's not what you really mean. – Scott Hannen Jun 12 '19 at 15:40

2 Answers2

-2

var ext = "_00";
if (/^_st$|^_mt$|^_00$/.test(ext) ){
    console.log("do something");
   //do something
}

Using regex this is possible.

Explanation: Each comparison is separated by an |(or) and enclosed in ^ and $ to match exactly otherwise it would match else where.

Nagaraju
  • 1,853
  • 2
  • 27
  • 46
-2

let ext = "_mt";

if( ext.match( /_st|_mt|_00/g ) ) {
  console.log("Yes : type 1");
}


if( ["_st", "_mt", "_00"].includes( ext ) ) {
  console.log("Yes : type 2");
}

As @Shily said you can use an array and this kind of regex too