← Back to JavaScript
08

Control flow: design clear decisions with if, else if and switch

Learn how to model business decisions with clean branches, avoid chaotic nesting and debug conditions so your code takes the right path every time.

📘 Theory

Types of decisions in real frontend work

Not every condition should be solved the same way. Picking the right structure simplifies everything.

A simple rule usually fits in an `if`. When you have several routes ordered by priority, `else if` works better. If you compare the same value against many discrete cases, `switch` is often the cleanest option.

The goal of this block is to stop improvising structures and start designing the flow with intention.

1

if

For quick binary decisions.

  • Authenticated user or guest
  • Valid field or invalid field
2

else if

For ordered priorities or ranges.

  • Classify a grade: excellent, pass, fail
  • Determine a risk level
3

switch

For discrete states.

  • Order state: pending, shipped, delivered
  • Role: admin, editor, visitor
4

Guard clauses

Exit early when preconditions are not met.

  • Avoid deep nesting
  • Improve readability and debugging

if and else if: order rules by priority

When several conditions might match, the order matters a lot.

JavaScript evaluates from top to bottom and runs the first true condition. If you place a broad rule first, it may block more specific rules below it.

In business logic, it is usually better to start with critical or restrictive cases and then move toward broader ones.

  • Think about evaluation order, not just isolated conditions.
  • Name variables with business meaning such as `openMinutes` or `priority`.
  • Avoid nested `if` blocks when an `else if` chain can express the rule more clearly.

switch: ideal for closed sets of states

If you compare one variable against many specific values, switch is often easier to read.

1

`switch` stands out when you work with UI states, notification types, payment methods or roles. Each `case` becomes an explicit business branch.

2

Do not forget `break`, or the execution may fall into the next case by accident.

Guard clauses: less nesting, more clarity

Reject invalid cases first and keep the happy path clean.

1

A guard clause is an early return from a function when the preconditions are not met. It is a very professional pattern because it lowers cognitive load.

Frequent flow mistakes and how to debug them

If the result is wrong, the bug is usually in the condition or its order.

1

Error one: broad conditions placed too early. Error two: forgetting `break` in a `switch`. Error three: comparing mixed types without normalizing the input first.

2

Recommended debugging method: print the input value, its type and the chosen branch so you can trace the exact execution path.

🧭 Key visuals

Conditionals in JavaScript

It shows execution paths based on business conditions.

Flow diagram using if, else if and else branches

A basic decision tree

It reinforces the mental model of a conditional as a split in the program flow before you write longer chains of `if` and `else if`.

Visual decision tree with one condition and two possible branches depending on whether it passes or fails

🧪 Learn by doing

Example Guided example: login flow by attempt count Handle success, lockout and retry routes with `if` and `else if`.
Example Guided example: switch for payment method Map each method to a UX message using `switch` plus `default`.
Example Interactive demo: route support tickets Choose a ticket type and urgency, and the system will decide the queue and estimated response time.

🏁 Challenges

Challenge Challenge 1: access validation by age Build a flow that handles invalid values first and then decides access.
Challenge Challenge 2: subscription plan switch Assign benefits by plan using `switch` and a `default` fallback.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com