JavaScript Academy · Lesson

if/else and switch — core patterns

Write clear conditions with if/else and switch; use guard clauses for early exits.

Lesson 1 of 38 steps

if/else and switch — core patterns is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.

Intro

Goal: Choose the right conditional. Start with if/else, add else if for multiple branches, and use switch for clean value matching.

if/else and switch — core patterns — illustration 1

if/else basics

If the condition is true, run the first block; otherwise run the else block.

// if/else — choose between two paths
const score = 72;

if (score >= 60) {
  console.log("Pass");
} else {
  console.log("Retry");
}
if/else and switch — core patterns — illustration 2

else if chain

Use else if for ordered checks from most specific to least.

// else if — three or more branches
const temp = 18;

if (temp >= 30) {
  console.log("Hot");
} else if (temp >= 15) {
  console.log("Mild");
} else {
  console.log("Cold");
}
if/else and switch — core patterns — illustration 3

switch basics

switch matches exact values. Remember to break to avoid falling through.

// switch — clean matching on a single value
const day = "Mon";

switch (day) {
  case "Mon":
    console.log("Start strong");
    break;
  case "Fri":
    console.log("Finish line");
    break;
  default:
    console.log("Keep going");
}
if/else and switch — core patterns — illustration 4

Guard clause

Use a guard clause to exit early when inputs are not valid; keeps the happy path clear.

// Guard clause — return early for invalid input
function greet(name) {
  if (!name) {
    console.log("Name required");
    return;
  }
  console.log("Hello, " + name + "!");
}

greet("");
greet("Ayla");
if/else and switch — core patterns — illustration 5

Tips & mistakes

Tips:

  • Order checks from most specific to least.
  • Keep conditions short and readable.
  • In switch, include a default case.
if/else and switch — core patterns — illustration 6

switch comparison quiz

Quick check: switch matching.

if/else and switch — core patterns — illustration 7

Recap

Recap: Use if/else for two paths, else if for ordered branches, switch for exact value matches, and guard clauses for early exits.

if/else and switch — core patterns — illustration 8
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “if/else and switch — core patterns” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “if/else and switch — core patterns”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.

What will I learn in “if/else and switch — core patterns”?

Write clear conditions with if/else and switch; use guard clauses for early exits. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “if/else and switch — core patterns” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. if/else and switch — core patterns
  2. Loops: for, while, do…while, for…of vs for…in
  3. Early Returns and Guard Clauses
← Back to JavaScript Academy