Back to subject
JavaScript/Conditionals: if, else, switch

Turn a Number into a Letter Grade

Practical lesson

Let's trace exactly which branch this if/else if chain takes, and why.

JavaScript checks score >= 90 first. Since score is 85, this is false, so it skips straight to the next condition without running anything inside this block.

Next it checks score >= 80. Since 85 is indeed greater than or equal to 80, this condition is true its block runs (grade = "B";), and critically, every remaining else if and else in the chain is now skipped entirely, even though score >= 70 would also technically be true.

This "stop at the first true condition" behavior is exactly why order matters in an else if chain: conditions must generally go from most specific/highest to least specific/lowest when checking ranges like this if you'd written score >= 70 before score >= 80, students who should get a "B" would incorrectly get a "C" instead.

The final output reads "Score: 85 → Grade: B" trace it yourself with score = 95 and score = 60 to confirm you understand exactly which branch each value takes.

Try it yourself
Code

Starter example

Code
<script>
  const score = 85;
  let grade;
  if (score >= 90) {
    grade = "A";
  } else if (score >= 80) {
    grade = "B";
  } else if (score >= 70) {
    grade = "C";
  } else {
    grade = "F";
  }
  document.body.innerHTML = `<p>Score: ${score} → Grade: ${grade}</p>`;
</script>
Live preview