javascript conditional logical operator

logical operators javascript
ternary operator javascript
javascript string comparison
javascript comparison operators
logical or
javascript vs
types of logical operators
logical operators definition

I have the following:

var cond1 = (a>b) ? true: false;
var cond2 = (d<c) ? true: false;
var cond3 = (e==f) ? true: false;

var found = cond1 && cond2 && cond3;

I want to be able in an elegant way to substitute the && logical operator for another like || and do this programmatically, as in a sort of "variable" operator. Because I don't want to do something like:

var found1 = cond1 && cond2 && cond3;
var found2 = cond1 || cond2 || cond3;
var found3 = (cond1 && cond2) || cond3;

and then switch/if depending on what choice the user has selected from the interface, it's not elegant.

How can I do this ? (if possible)

Thank you in advance


I would start with something like a method which can take a string (AND/OR) and an array of bool delegates and perform the action(s)

function operator(which, ...args)
{
    switch(which.toUpperCase())
    {
        case "AND": return args.every(a => a());
        case "OR": return args.some(a => a());
        default: throw "Invalid operator " + which;
    }
}

var a = 1;
var b = 2;
var c = 3;
var d = 4;
var e = 5;
var f = 5;

var cond1 = () => (a>b) ;
var cond2 = () => (d<c) ;
var cond3 = () => (e==f);

var found1 = operator("and",cond1,cond2,cond3);
var found2 = operator("or",cond1,cond2,cond3);
var found3 = operator("or", () => operator("and",cond1,cond2), cond3);
console.log(found1,found2,found3)

Logical operators, This chapter describes JavaScript's expressions and operators, including Bitwise operators; Logical operators; String operators; Conditional  The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement. The source for this interactive example is stored in a GitHub repository.


I think elegant way of writing this is code should be readable and easy to understand. Making a logic complex to achieve simple thing doesn't make sense in my opinion. Things which kept simple and clean is less bug prone than complex logic, it gives us the confidence on our code.

Here you can store && and || in variable and can pass to the function.

var cond1 = (2>3);
var cond2 = (5<9);
var cond3 = (5==7);

const compareConditions = (operator, operand1, operand2) => {
  switch(operator){
    case "&&": {
      return operand1 && operand2;
    }
    case "||":{
       return operand1 || operand2;
    }
    default:{
      return false;
    }
  }
}

console.log(compareConditions("||", compareConditions("&&" , cond1, cond2), cond3));

Expressions and operators, There are three logical operators in JavaScript: || (OR), && (AND), ! Write an “if” condition to check that age is between 14 and 90 inclusively. Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they will return a non-Boolean value.


Depending on complexity, I'd either go with something similar to what you already have, or if there are a lot of options, I might go with some sort of flags/bitmask solution.

There are some good examples of this in JavaScript here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

Logical operators, In JavaScript, the logical operators have different semantics than other and loops will be executed when the condition evaluates to a "truthy"  The so-called “conditional” or “question mark” operator lets us do that in a shorter and simpler way. The operator is represented by a question mark ?. Sometimes it’s called “ternary”, because the operator has three operands. It is actually the one and only operator in JavaScript which has that many.


The && and || Operators in JavaScript, The logical operators. In formal logic, only a few operators exist: negation, conjunction, disjunction, implication, and bicondition. Each of these has  Conditional operator “if”. If I were to rank my favorite ways to write conditionals in JavaScript, ternary operator would certainly be at the top of that list. Logical assignment uses


A definitive guide to conditional logic in JavaScript, Its result will be based in whether the value is truthy or falsy: First, let's identify that there are three logical operators in JavaScript: ! (NOT), && (  It always evaluate to true because, what && operator does is to check both condition, if both condition is true than the block will get executed, otherwise else block will get executed. % will return remainder which will never be zero. in your case while calculate modules i.e 7%2 will result 1. either you need to use logical !! operator or you


JavaScript: Logical operators and Boolean Values, The AND ( && ) and OR ( || ) logical operators both require two operands, and are used to perform Boolean operations on their operands. Given  JavaScript logical operators covering description, example code, output of example, online practice editor and explanation by w3resource.com