Conditional Logical Operators Proposal

The Problem

JavaScript developers frequently need to apply different logical operators based on runtime conditions. This pattern appears regularly in form validation, permission systems, feature flags, and API handling, but currently requires verbose code:

// Current approach: repetitive ternary expressions
const isValid = field.isRequired 
  ? (field.value && field.value.length > 0)
  : (!field.value || field.value.length > 0);

// Alternative: multiple conditional branches
let canAccess;
if (strictMode) {
  canAccess = user.hasRole && user.isOwner;
} else {
  canAccess = user.hasRole || user.isOwner;
}

Proposed Solution

Introduce conditional logical operators that allow dynamic operator selection:

const isValid = field.value ?(field.isRequired ? && : ||) field.value.length > 0;
const canAccess = user.hasRole ?(strictMode ? && : ||) user.isOwner;

Syntax

expression1 ?(condition ? operator1 : operator2) expression2

The operator selection follows the same pattern as the existing ternary conditional operator.

Benefits

β†’ Reduces code duplication - eliminates repeated logic branches
β†’ Improves readability - single expression instead of multiple statements
β†’ Natural syntax - extends the familiar ? : conditional pattern
β†’ Performance friendly - no additional function call overhead

The concept could extend to other binary operators:

// Comparison operators
const result = value ?(ascending ? < : >) threshold;

// Equality operators  
const matches = input ?(strict ? === : ==) expected;

There's some languages out there where you can treat operators as functions by wrapping them in parentheses. If JavaScript were to support something like that, it could help solve your specific problem, and other similar problems.

const result = (ascending ? (<) : (>))(value, threshold);

(We conditionally decide if we want the less than or greater than function, then we call it with the two operands).

Alternatively, we could follow in Python’s footsteps - they don't let you convert an operator into a function per-say, but they do provide a module containing all built-in operators as functions.

Just a couple of other ways I can think of to solve the stated problem.

In my opinion, this drastically decreases readability. Things getting shorter is not an improvement if it makes the logic harder to parse. Just write the conditional, it makes it clear exactly what’s going on.

3 Likes

I guarantee you that, if it will be a feature, will be used quite a lot

Hi!

The Problem

JavaScript developers frequently need to apply different logical operators based on runtime conditions. This pattern appears regularly in form validation, permission systems, feature flags, and API handling, but currently requires verbose code:

This is typically solved with the strategy
pattern
, especially
since it's not always just logical operators.

Your examples should be written as

const strategy = field.isRequired
   ? v => v && v.length > 0
   : v => !v || v.length > 0;
const isValid = strategy(field.value);

and

const strategy = strictMode
   ? ({hasRole, isOwner}) => hasRole && isOwner
   : ({hasRole, isOwner}) => hasRole || isOwner;
const canAccess = strategy(user);

Btw, in your first example, if field.value is a string (or even a
string-or-undefined) then (!field.value || field.value.length > 0) is
always true. You can therefore simplify the statement to just

const isValid = !field.isRequired || field.value && field.value.length > 0;
// or further:
const isValid = !field.isRequired || !!field.value;
const isValid = !field.isRequired || field.value?.length > 0;

kind regards,
Bergi