25 min·beginner

Lesson 1

Conditions & branching

Build a practical mental model for conditions & branching and use it in working JavaScript.

By the end, you will be able to…

  • Explain conditions & branching in your own words
  • Use conditions & branching in a small program
  • Recognize and debug a common failure mode

Build the mental model

if/else branches let a program take different paths depending on a condition. Each branch should return or assign consistently so the function's result is predictable no matter which path executes.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: Eligible

Why this matters

Almost every real feature — permissions, validation, pricing tiers — is a tree of conditions; writing them clearly is a core professional skill.

Real-world example

A checkout flow deciding free vs. paid shipping based on cart total is a chain of conditions exactly like this one.

Common mistakes

  • Forgetting an else branch and returning undefined unexpectedly
  • Deeply nesting ifs instead of returning early
  • Comparing with = instead of === inside a condition

Try it yourself

Challenge

Extend canVote to also handle the exact boundary age of 18.

Practice

Knowledge check

What is a common bug when writing multi-branch conditions?

Summary

Use if/else to branch cleanly, and prefer early returns over deep nesting.