Skip to content

useElseIf (JavaScript)

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

Require else if when adjacent if statements compare the same value against different values.

When two if statements in a row compare the same variable or property with === against different values, only one of them can match. Joining them with else if makes this clear, and skips the second check once the first one matched.

Conditions like if (flag) and if (!flag) count as comparisons with true and false when flag is declared as a boolean.

The rule ignores an if statement when a previous branch ends with return, throw, break, or continue, or assigns a new value to the compared variable or property.

The fix is unsafe, because a function called in a previous branch could change the compared value.

if (status === "loading") {
showSpinner();
}
if (status === "error") {
showError();
}
code-block.js:4:1 lint/nursery/useElseIf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This if statement compares the same value as the previous one, but against a different value.

2 │ showSpinner();
3 │ }
> 4 │ if (status === "error") {
│ ^^^^^^^^^^^^^^^^^^^^^^^
5 │ showError();
6 │ }

ℹ The previous condition is here.

> 1 │ if (status === "loading") {
│ ^^^^^^^^^^^^^^^^^^^^
2 │ showSpinner();
3 │ }

ℹ Only one of these conditions can be true, but separate if statements hide this, and the second condition is still checked when the first one matched.

ℹ Add else before this if statement to join it to the previous one.

ℹ 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: Add else before this if statement.

1 1 │ if (status === "loading") {
2 2 │ showSpinner();
3 │ - }
4 │ - if·(status·===·"error")·{
5 │ - ····showError();
6 │ - }
3 │ + }·else·if·(status·===·"error")·{
4 │ + ····showError();
5 │ + }
7 6 │

if (event.key === "ArrowUp" || event.key === "ArrowLeft") {
previous();
} else if (event.key === "ArrowDown") {
next();
}
if (event.key === "Escape") {
close();
}
code-block.js:6:1 lint/nursery/useElseIf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This if statement compares the same value as the previous one, but against a different value.

4 │ next();
5 │ }
> 6 │ if (event.key === "Escape") {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
7 │ close();
8 │ }

ℹ The previous condition is here.

1 │ if (event.key === "ArrowUp" || event.key === "ArrowLeft") {
2 │ previous();
> 3 │ } else if (event.key === "ArrowDown") {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
4 │ next();
5 │ }

ℹ Only one of these conditions can be true, but separate if statements hide this, and the second condition is still checked when the first one matched.

ℹ Add else before this if statement to join it to the previous one.

ℹ 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: Add else before this if statement.

3 3 │ } else if (event.key === "ArrowDown") {
4 4 │ next();
5 │ - }
6 │ - if·(event.key·===·"Escape")·{
7 │ - ····close();
8 │ - }
5 │ + }·else·if·(event.key·===·"Escape")·{
6 │ + ····close();
7 │ + }
9 8 │

function toggle(enabled: boolean) {
if (enabled) {
start();
}
if (enabled === false) {
stop();
}
}
code-block.ts:5:5 lint/nursery/useElseIf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This if statement compares the same value as the previous one, but against a different value.

3 │ start();
4 │ }
> 5 │ if (enabled === false) {
│ ^^^^^^^^^^^^^^^^^^^^^^
6 │ stop();
7 │ }

ℹ The previous condition is here.

1 │ function toggle(enabled: boolean) {
> 2 │ if (enabled) {
│ ^^^^^^^
3 │ start();
4 │ }

ℹ Only one of these conditions can be true, but separate if statements hide this, and the second condition is still checked when the first one matched.

ℹ Add else before this if statement to join it to the previous one.

ℹ 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: Add else before this if statement.

2 2 │ if (enabled) {
3 3 │ start();
4 │ - ····}
5 │ - ····if·(enabled·===·false)·{
6 │ - ········stop();
7 │ - ····}
4 │ + ····}·else·if·(enabled·===·false)·{
5 │ + ········stop();
6 │ + ····}
8 7 │ }
9 8 │

if (status === "loading") {
showSpinner();
} else if (status === "error") {
showError();
}
// The conditions compare different variables.
if (status === "loading") {}
if (mode === "dark") {}
// Both conditions can be true at the same time.
if (count === 1 || count === 2) {}
if (count === 2) {}
// The first branch changes the compared value.
if (step === 1) {
step = 2;
}
if (step === 2) {}
function handle(signal) {
// The first branch always leaves the function.
if (signal === "SIGINT") {
return;
}
if (signal === "SIGTERM") {}
}
  • If you want to remove else after a branch that ends with return or throw, see noUselessElse.
  • If you want to turn else { if (...) {} } into else if (...) {}, see useCollapsedElseIf.