else in JavaScript: the branch that runs when the condition is false

else runs a block when the condition of the if is false, and chains into else if to handle several cases that exclude one another.
3 min read
Believemy logo

else never stands alone. It completes an if by taking on everything the condition left out, which guarantees that exactly one of the two branches will be walked.

It is also the keyword people write too often. A fair share of the else blocks in a codebase can disappear without changing any behavior, and the code always gains from it.


Definition

else sits immediately after the block of an if and runs its own block when the condition evaluated to false. It carries no condition of its own: it collects every other case.

JAVASCRIPT
const stock = 0;

if (stock > 0) {
  console.log("In stock");
} else {
  console.log("Out of stock");   // this is the branch taken
}

The two blocks exclude each other: the engine never runs more than one, then resumes after the closing brace of the second.


The else if chain

else if is not a keyword, it is an if written inside the else of the previous one. The chain reads from top to bottom and stops at the first true condition, which makes the order of the tests decisive.

JAVASCRIPT
function fare(age) {
  if (age < 12) {
    return "child";
  } else if (age < 26) {
    return "young";
  } else {
    return "full fare";
  }
}

console.log(fare(8), fare(20), fare(40));   // child young full fare

Swapping the first two conditions would break the function without raising a single error: an eight-year-old would pass the age < 26 test and walk away with the young fare. When the cases are numerous and compared against one value, a switch expresses the same intent in a flatter way.


When to do without it

As soon as a branch ends with a return, the else becomes pointless: the function has already left. Removing it strips one level of indentation per case handled.

JAVASCRIPT
function fare(age) {
  if (age < 12) return "child";
  if (age < 26) return "young";
  return "full fare";
}

The short version reads like a list of rules, from the narrowest case to the general one. The long version forces the reader through three levels of nesting to reach the same information.

Good to know

Without braces, else governs only the statement immediately following it. A second line aligned underneath no longer belongs to the branch and runs in every case, with no message to point it out. That is why braces are required even for a single line.


Frequently asked questions

Question

Should an else always be written?

No. An empty else, or one repeating what the code already does by default, adds noise without information. The useful question is different: does the opposite case deserve handling of its own? If it does, the branch belongs there; if not, its absence says exactly the same thing.


Question

How does it differ from the ternary operator?

The Ternary operator (?:) picks between two values, while if and else pick between two blocks of statements. To assign a variable depending on a condition, the ternary fits on one line and reads better. As soon as a branch does several things, the block form takes the lead again.


Question

How do you handle a missing value rather than a false condition?

An if followed by an else treats zero and the empty string as absences, which is rarely intended. To replace only null and undefined, Nullish coalescing (??) is built for the job and lets through legitimate values that happen to be falsy.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.