-1

Can i use operators like:

"wo" == ("man" || "wo");

or

x = ("man" || "wo");
"wo" == x;

if not is there any other technique that I can use?

Purpose was to save the length of code with different values in condition.

if(orderSubType == (sdcConstants().orderSubType.UNBAR_0 || 
                    sdcConstants().orderSubType.UNBAR_00 || 
                    sdcConstants().orderSubType.UNBAR_OG) && 
                    item.source == sdcConstants().source.EXT && 
                    item.code == sdcConstants().CFSS.CFSS_VOICE_DUNNING){
       item.state = sdcConstants().servAction.DELETE;
}else{
       item.state = sdcConstants().servAction.NO_OPERATION;
}
Zee
  • 8,420
  • 5
  • 36
  • 58
  • as far as I know no, because `"man" || "wo"` means `if "man" is falsey, then "wo", else "man"`. For such a task you might want to either use ~.indexOf(element) in an array or .hasOwnProperty on an object that contains the properties you want to check. – briosheje Apr 16 '15 at 10:10
  • format it as : `if("wo" == "man" || "wo" == "wo") { ... }` – Vikrant Apr 16 '15 at 10:15
  • @Qantas94Heavy: Well, it's close to what I wanted. This code does OR Operator. Is it applicable to AND Operator as well? x = ["man", "wo"]; if (x.indexOf("wo") != -1) { alert('found'); } – Muneeb Ali Apr 16 '15 at 10:24
  • @MuneebAli: well `x` cannot be two things at once, so I don't see the point of having an AND operator. Also see this question: http://stackoverflow.com/questions/13737091/concise-way-to-compare-against-multiple-values – Qantas 94 Heavy Apr 16 '15 at 10:26

2 Answers2

1

Can i use operators like:

"wo" == ("man" || "wo");

Not for what you want that to do, no.

Purpose was to save the length of code with different values in condition...

The usual two approaches for dealing with testing against multiple values are switch or a map, but as your condition is a compound of different properties and different logic operators, it's not going to buy you much.

Here's an example with switch (and yes, in JavaScript, those case statements are correct, JavaScript is not limited to constants like Java or C):

switch (orderSubType) {
    case sdcConstants().orderSubType.UNBAR_0:
    case sdcConstants().orderSubType.UNBAR_00:
    case sdcConstants().orderSubType.UNBAR_OG:
        if (item.source == sdcConstants().source.EXT && item.code == sdcConstants().CFSS.CFSS_VOICE_DUNNING) {
            item.state = sdcConstants().servAction.DELETE;
            break;
        }
        //FALL THROUGH TO DEFAULT
    default:
        item.state = sdcConstants().servAction.NO_OPERATION;
        break;
}

You can see why I don't think it buys you anything over the if version in this particular case:

if (    (orderSubType == sdcConstants().orderSubType.UNBAR_0 ||
         orderSubType == sdcConstants().orderSubType.UNBAR_00 ||
         orderSubType == sdcConstants().orderSubType.UNBAR_OG
        )
        && item.source == sdcConstants().source.EXT
        && item.code == sdcConstants().CFSS.CFSS_VOICE_DUNNING
    ) {
    item.state = sdcConstants().servAction.DELETE;
} else {
    item.state = sdcConstants().servAction.NO_OPERATION;
}
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Can't this be done in an inline ternary operator using an array and indexOf? – briosheje Apr 16 '15 at 10:32
  • @briosheje: It can be done with `indexOf` as well, if you want to create an unnecessary array and make an unnecessary method call. You don't need a ternary if you do that. – T.J. Crowder Apr 16 '15 at 10:35
  • That was for the "Purpose was to save the length of code with different values in condition..." point :P – briosheje Apr 16 '15 at 10:36
  • @briosheje: Ah, yes, I see what you're thinking: `item.state = conditionsHere ? sdcConstants().servAction.DELETE : sdcConstants().servAction.NO_OPERATION;` At that point, with as many conditions as we have here, to my mind you're running into serious readability/debugging/maintenance issues. – T.J. Crowder Apr 16 '15 at 10:36
  • exactly. something like that: `item.state = (([sdcConstants().orderSubType.UNBAR_0, sdcConstants().orderSubType.UNBAR_00,sdcConstants().orderSubType.UNBAR_OG].indexOf(orderSubType) + 1) && item.source == sdcConstants().source.EXT && item.code == sdcConstants().CFSS.CFSS_VOICE_DUNNING ) ? sdcConstants().servAction.DELETE : sdcConstants().servAction.NO_OPERATION;` . It's terrible if you look at it, but it's still inline :P – briosheje Apr 16 '15 at 10:40
0

You could use an array and check if the item exists in the array. For example,

x = ["man", "wo"];
if (x.indexOf("wo") != -1) {
    alert('found');
}
cyber_rookie
  • 665
  • 5
  • 9
  • The `in` operator only iterates over the [properties of the array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in). This will not work. – Phylogenesis Apr 16 '15 at 10:15
  • I edited my answer. This works. Check this fiddle for its working: http://jsfiddle.net/14kjzmqh/ – cyber_rookie Apr 16 '15 at 10:16