health-check domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260JavaScript Foundations β’ StreetGeek Academy
Learn how to control the flow of your code with conditionals and loops β enabling your portfolio to respond to different conditions and user interactions.
Control Flow determines what happens next in your code. JavaScript runs top-to-bottom β but conditionals and loops can modify that path.
| Type | What It Does |
|---|---|
| Conditional statements | Make decisions (if, else, switch) |
| Loops | Repeat actions (for, while, for...of) |
if, else if, and else Statementslet score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else {
console.log("Grade: C or below");
}
let projects = 7;
if (projects >= 10) {
console.log("Pro Developer π§ ");
} else if (projects >= 5) {
console.log("Rising Star π");
} else {
console.log("Beginner β‘");
}
Some values in JavaScript automatically evaluate as false in conditionals.
Falsy values:
let username = "";
if (username) {
console.log("Welcome back!");
} else {
console.log("Please enter your name.");
}
Use switch when checking multiple possible values of the same variable.
let theme = "dark";
switch (theme) {
case "dark":
console.log("Dark mode activated π");
break;
case "light":
console.log("Light mode activated βοΈ");
break;
default:
console.log("System default mode βοΈ");
}
break to prevent fall-through.
Loops let you repeat code without duplication.
for Loopfor (let i = 1; i <= 5; i++) {
console.log(`Project ${i}`);
}
while Looplet count = 1;
while (count <= 3) {
console.log(`Saving file ${count}`);
count++;
}
for...of Looplet projects = ["Portfolio", "Blog", "ShopBlocks"];
for (let project of projects) {
console.log(`Working on: ${project}`);
}
<p id="level-badge"></p>
const levelBadge = document.getElementById("level-badge");
let completedProjects = 8;
let badge;
if (completedProjects >= 10) {
badge = "Gold";
} else if (completedProjects >= 5) {
badge = "Silver";
} else {
badge = "Bronze";
}
levelBadge.textContent = `π
Your Level: ${badge}`;
<p id="day-greeting"></p>
const dayGreeting = document.getElementById("day-greeting");
let day = new Date().getDay();
let message;
switch (day) {
case 0: message = "Relax, itβs Sunday! π"; break;
case 1: message = "Motivation Monday πͺ"; break;
case 2: message = "Tech Tuesday π§"; break;
case 3: message = "Web Wednesday π»"; break;
case 4: message = "Thriving Thursday π"; break;
case 5: message = "Focus Friday π"; break;
case 6: message = "Social Saturday π"; break;
}
dayGreeting.textContent = message;
<ul id="project-list"></ul>
const projectList = document.getElementById("project-list");
const projects = ["Portfolio", "HTML Course", "JavaScript Demo", "WPNfinite Blog"];
for (let i = 0; i < projects.length; i++) {
let li = document.createElement("li");
li.textContent = `${i + 1}. ${projects[i]}`;
projectList.appendChild(li);
}
let mode = "dark";
let emoji = mode === "dark" ? "π" : "βοΈ";
console.log(`Current mode: ${emoji}`);
Same as:
if (mode === "dark") {
emoji = "π";
} else {
emoji = "βοΈ";
}
1) Whatβs the difference between if and switch?
2) What keyword stops a switch case?
3) Name all falsy values in JavaScript.
4) How many times will this loop run?
for (let i = 1; i < 4; i++) {
console.log(i);
}
5) Whatβs the shorthand version of:
if (x) { y = "Yes"; } else { y = "No"; }?
<p id="xp-tracker"></p>
const xpTracker = document.getElementById("xp-tracker");
let projects = 7;
let skills = ["HTML", "CSS", "JavaScript"];
let message = "";
if (projects >= 10) {
message = "Pro Developer π§ β Youβve mastered multiple projects!";
} else if (projects >= 5) {
message = "Rising Star π β Keep going, youβre leveling up!";
} else {
message = "Beginner β‘ β Every expert started here!";
}
xpTracker.textContent = `${message} Skills: ${skills.join(", ")}.`;
if, else, and switchNext up β MODULE 4: Functions & Scope β Writing Reusable Code