Sikau

JavaScript · Day 13 · about 25 min

else if chains

Choosing between many options.

More than two roads

Exam grades aren't just pass or fail — there's A, B, C and more. When there are several choices, you can chain decisions together with else if.

else if

const marks = 72;

if (marks >= 80) {
  console.log("Grade A");
} else if (marks >= 60) {
  console.log("Grade B");
} else if (marks >= 40) {
  console.log("Grade C");
} else {
  console.log("Try again");
}

JavaScript checks each condition from top to bottom and runs the first block whose condition is true. Everything after that is skipped.

The rest of this lesson

  • Order matters in a chainexplain
  • Predict the outputpredict
  • Build the ticket pricesorder
  • Your turnwrite code
  • Challengechallenge
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript