Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

When does JavaScript use the `else` branch of an `if` statement?

Quick answer

JavaScript runs an if block when its condition is truthy and the optional else block when the condition is falsy. Zero is falsy, so if (0) takes the else branch. Other falsy values include false, null, undefined, NaN, and the empty string. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

When exactly does JavaScript run the else branch? I understand that it has something to do with the condition, but I’m not sure what happens when the condition is a value rather than a comparison.

Geez

JavaScript evaluates the condition inside if (...) as truthy or falsy. If it is truthy, the if block runs; if it is falsy, the optional else block runs. Zero is falsy, so if (0) takes else. Other falsy values include false, null, undefined, NaN, and the empty string. [S1]

if (0) {
  console.log("if branch");
} else {
  console.log("else branch");
}

This prints else branch. The branch depends on the condition’s truthiness, not on whether the value is the word “false.”

Follow-up question

Can you show the same branching logic with two inputs, one of them zero, and tell me the output for each?

Geez

Here are two inputs that are both falsy values:

function describe(value) {
  if (value) {
    console.log("if branch");
  } else {
    console.log("else branch");
  }
}

describe(0);
describe(false);

For 0, the output is else branch. For false, the output is also else branch, because both values are falsy. [S1] This demonstrates why if (value) is a truthiness check rather than a check for one particular value.

Follow-up question

I’m using zero to mean “no items,” but I also need to distinguish that from a missing value. Should I still write if (count)?

Geez

With if (count), a falsy value such as 0 takes the else branch. The listed falsy values also include undefined and null, so this check does not by itself tell you which of those values you have. [S1]

If zero and a missing value need different handling, decide which specific values your program should distinguish and write conditions for those cases. For example, you could first check whether the value is the kind of missing value you expect, then handle zero separately. The right conditions depend on how your program represents missing data; truthiness alone groups several values together.

Follow-up question

What quick check can I use when I’m unsure which branch will run?

Geez

Read the expression inside the parentheses after if and ask whether that expression is truthy or falsy. A truthy condition selects the if block; a falsy condition selects the else block if one is present. [S1]

For if (value), substitute 0: zero is falsy, so the else branch runs. For if (false), the condition is also falsy, so else runs. [S1] This quick check is especially useful when a value such as zero has meaning in your program: make sure the truthiness behavior matches the distinction you want.

A practical checklist

  • Identify the expression inside if (...).

  • Determine whether that expression is truthy or falsy.

  • If the condition is falsy, the else branch runs when present. [S1]

  • Remember that 0, false, null, undefined, NaN, and the empty string are falsy. [S1]

  • Check whether truthiness groups values that your program needs to distinguish.

A few more useful answers

Does `else` run only when the condition is the number zero?

No. It runs when the condition is falsy. Zero is one example; false, null, undefined, NaN, and the empty string are also listed as falsy values. [S1]

If the condition is `false`, does the `else` branch run?

Yes. false is a falsy value, so it selects the else branch when one is present. [S1]

What if there is no `else` block?

The else branch is optional. If there is no else block, there is no alternate block to run when the condition is falsy.

Sources

References used for this guide.

  1. [S1] if...else — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work