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
switchstatement
The pages
Eleven pages, about 13 minutes.
- 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…
- 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…
- 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…
- 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…
- Combining Conditions Remember the logical operators from Grade 3? They're perfect inside if statements.
- Nested Decisions You can put an if statement inside another if statement. This is called nesting. Nesting works, but be careful. When…
- 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…
- 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…
- 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…
- 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…
- 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.