Making Decisions: if, else & Friends

Your code learns to choose its own adventure. Eleven short pages from Grade 4 of Elementary School, about 13 minutes of reading in all.

What you’ll learn

  • How to run code only when something is true
  • Backup plans with else
  • Multiple choices with else if
  • Truthy and falsy values
  • Combining conditions (and the classic OR bug)
  • Nested decisions
  • The ternary operator ? :
  • The switch statement

The pages

Eleven pages, about 13 minutes.

  1. Making Decisions with if Every day, you make hundreds of tiny decisions without even thinking about them. If it's raining, take an umbrella. If…
  2. else: The Backup Plan Often you want to do one thing if the condition is true, and something else if it's false. That's what else is for…
  3. else if: More Than Two Choices Life isn't always a simple yes-or-no. Sometimes there are several possibilities. For that, you can chain conditions…
  4. Truthy and Falsy Here's something interesting. The condition inside an if doesn't have to be an actual boolean. You can put any value…
  5. Combining Conditions Remember the logical operators from Grade 3? They're perfect inside if statements.
  6. Nested Decisions You can put an if statement inside another if statement. This is called nesting. Nesting works, but be careful. When…
  7. The Ternary Operator: if/else in One Line Sometimes you just want to pick between two values based on a condition. Writing a full if/else for that feels like a…
  8. The switch Statement When you're comparing one value against a list of specific options, a long chain of else if can get repetitive. For…
  9. Mini Project: The Cinema Ticket Machine Let's build the brain of a cinema ticket machine. Ticket prices depend on the customer's age, whether it's a matinee…
  10. Mini Project: The Magic 8-Ball Remember the random number homework from Grade 3? Let's use it to build a fortune teller. Ask it a yes-or-no question…
  11. Recap The key ideas of Grade 4, JavaScript if and else, on one page. if (condition) { ... } runs a block of code only when…

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning