Skip to content

noUselessNestedTernary (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUselessNestedTernary": "error"
}
}
}
}

Disallow nested ternary expressions that can be merged into one.

A ternary expression (condition ? a : b) placed inside another one is unnecessary when both of them share a result. The two conditions can then be combined with && or ||, so that one ternary expression is enough. This makes the code shorter and easier to read.

The rule also checks chains of ternary expressions whose conditions compare the same value with === against different literals, such as x === 1 ? a : x === 2 ? b : c. At most one of these conditions is true at a time. A condition can then be combined with a later condition that has the same result, or removed when its result is also the last result of the chain.

The rule doesn’t report code where the simplification would change whether or when a function call or an assignment in a condition runs.

const result = a ? (b ? c : d) : d;
code-block.js:1:16 lint/nursery/noUselessNestedTernary  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This nested ternary expression is unnecessary.

> 1 │ const result = a ? (b ? c : d) : d;
│ ^^^^^^^^^^^^^^^^^^^
2 │

ℹ The nested expression repeats a result of the expression around it. Combining both conditions with && gives the same results without the nesting.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Combine the conditions with &&.

1 │ - const·result·=·a·?·(b·?·c·:·d)·:·d;
1 │ + const·result·=·a·&&·b·?·c·:·d;
2 2 │

const result = a ? d : (b ? d : c);
code-block.js:1:16 lint/nursery/noUselessNestedTernary  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This nested ternary expression is unnecessary.

> 1 │ const result = a ? d : (b ? d : c);
│ ^^^^^^^^^^^^^^^^^^^
2 │

ℹ The nested expression repeats a result of the expression around it. Combining both conditions with || gives the same results without the nesting.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Combine the conditions with ||.

1 │ - const·result·=·a·?·d·:·(b·?·d·:·c);
1 │ + const·result·=·a·||·b·?·d·:·c;
2 2 │

const result = a ? (b ? d : c) : d;
code-block.js:1:16 lint/nursery/noUselessNestedTernary  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This nested ternary expression is unnecessary.

> 1 │ const result = a ? (b ? d : c) : d;
│ ^^^^^^^^^^^^^^^^^^^
2 │

ℹ The nested expression repeats a result of the expression around it. Combining both conditions with && gives the same results without the nesting.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Combine the conditions with &&.

1 │ - const·result·=·a·?·(b·?·d·:·c)·:·d;
1 │ + const·result·=·a·&&·!b·?·c·:·d;
2 2 │

const result = x === 1 ? a : (x === 2 ? b : (x === 3 ? a : c));
code-block.js:1:16 lint/nursery/noUselessNestedTernary  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The first condition of this chain of ternary expressions has the same result as a later condition.

> 1 │ const result = x === 1 ? a : (x === 2 ? b : (x === 3 ? a : c));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ Combining both conditions with || gives the same results with one ternary expression less.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Combine the conditions with ||.

1 │ - const·result·=·x·===·1·?·a·:·(x·===·2·?·b·:·(x·===·3·?·a·:·c));
1 │ + const·result·=·x·===·1·||·x·===·3·?·a·:·(x·===·2·?·b·:·c);
2 2 │

const combined = a && b ? c : d;
const threeResults = a ? (b ? c : d) : e;
const chain = x === 1 || x === 3 ? a : (x === 2 ? b : c);