JS-045
else if explained in a JavaScript learning illustration — Another condition to check
Beginner5 min~5 min read#else-if#conditions#branching

else if

Another condition to check

Goal

Learn to check several conditions in sequence using else if.

Explanation

  • else if lets you add another check after if.
  • If the first condition does not pass, JavaScript checks the next one.
  • You can create a chain of many conditions.
  • Only the first block whose condition is true will run.
  • else if is often used to choose one option from several.

Analogy

First, you check whether it is hot. If not, you check whether it is warm. If not, you check whether it is cold.

Code

if (isHot) {
  console.log("T-shirt");
} else if (isWarm) {
  console.log("Jacket");
} else {
  console.log("Coat");
}

Result: Jacket

Common mistake

Beginners may think every else if block runs. In fact, only the first block with a true condition runs.

Remember

  • else if adds another condition.
  • Conditions are checked from top to bottom.
  • After the first true condition, the remaining checks are skipped.

Quick Quiz

What does else if do?

Resources