if/else and switch — core patterns
Write clear conditions with if/else and switch; use guard clauses for early exits.
if/else and switch — core patterns is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 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");
}

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");
}

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");
}

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");

Tips & mistakes
Tips:
- Order checks from most specific to least.
- Keep conditions short and readable.
- In switch, include a default case.

switch comparison quiz
Quick check: switch matching.

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

Frequently asked questions
Is the “if/else and switch — core patterns” lesson free?
Yes — the full text of “if/else and switch — core patterns” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
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
- if/else and switch — core patterns
- Loops: for, while, do…while, for…of vs for…in
- Early Returns and Guard Clauses