Skip to content

no-else-return

JavaScript / TypeScriptstyleno fixdefault: off

Reports an else when the if and every else if before it end in return (what the else itself holds does not matter). An else if is not reported, and neither is a chain in which one of the earlier branches can fall through.

When every earlier branch returns, the code in the else can follow the if without the else, which removes one level of nesting and makes the early return plain.

To fix, delete the else and de-indent its body.

Reported
...
if (a) { b(); } else { if (c) { d(); } e(); }
if (a) { b(); } else if (c) { d(); }
function f(a) { if (a) { return 1; } else { return 2; } }
function g(a) { if (a) { return 1; } else if (b) { return 2; } }
function h(a) { if (a) { return 1; } else if (b) { return 2; } else { return 3; } }
function i(a) { if (a) { x(); } else { return 2; } }
...
var z = a ? b : false;
function j(a) { if (a) { if (b) { return 1; } else { return 2; } } else { return 3; } }
js:
lint:
rules:
no-else-return: warn # off (default), warn, error

Suppress one line with sudsy-disable-next-line no-else-return in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint no-else-return partial the allowElseIf option and the skip for an else that declares names are not supported; one extra report in the three.js corpus (98 matched)