Concise syntax for comparing multiple values

Problem

When multiple values need to satisfy the same condition, the condition has to be repeated:

if (a === 1 && b === 1 && c === 1) {}

This can become verbose when the same comparison is repeated for many values.

Proposed syntax

A possible syntax could be:

if ((a, b, c) === 1) {}

Similarly:

if (typeof (a, b, c) === "number") {}

The goal is to provide a concise way to apply the same comparison or type check to multiple expressions.

I'd like to get feedback on whether this is useful and whether there are better syntax or semantic alternatives.

no, it couldn't, because (a, b, c) is already valid syntax using the comma operator.

Either way, it sounds to me like the problem you're solving is at best a mild inconvenience, which is a low value for something with such a high cost as syntax.

you could always do [a, b, c].every(x => x === 1) if verbosity is a problem?

1 Like

[a, b, c].every(x => x === 1) is probably the right readability trade-off. If this is in a hot path where the temporary array matters, hoist the predicate: const isOne = x => x === 1; if (isOne(a) && isOne(b) && isOne(c)) {}. That keeps the comparison in one place without adding syntax that collides with the comma operator.

1 Like