JavaScript AcademyProfessional Engineering

Learning object 9 of 100

Conditional decision tree

Step through if, else if and else chains and see exactly which conditions are checked, which branch runs and which branches are skipped. Edit the input values and step again.

Loading…

What it shows

JavaScript checks conditions from the top and runs the first branch whose condition is true; the rest of the chain is skipped. Order matters: put the most specific or highest band first. The highlighted line shows the next statement, so you can see conditions that are never reached.

Guided experiment

  1. In the shipping program, change total to 120, then 20. Which line runs each time?
  2. Swap the order of the two else if conditions and predict what breaks.
  3. In the nested program, change isMember to false and step through again.

Predict

With score = 85 and the chain >= 90, >= 80, >= 70, which grade is printed?

Related

Lessons: Lesson 13: If, else if and else, Lesson 15: Building decision-making applications

Projects: Project 9: Shopping Cart Price Calculator, Project 1: Interactive Tip Calculator

Source: web/assets/js/objects/object-009.js in the companion repository.