Logic Brain Gym: Mastering Control Flow
This is where JavaScript stops being just a language and starts becoming a way of thinking.
At this stage, you either level up… or give up. We level up.
Conditions: if, else if, else
Logic in JavaScript starts with one simple question:
If this is true, what should happen? If not, then what?
if (condition) {
// runs if true
} else {
// runs if false
}Real-life example:
let age = 18;
if (age >= 17) {
console.log("You may enter the cinema 🎬");
} else {
console.log("Sorry, not old enough 😅");
}JavaScript has zero empathy. True runs. False gets skipped.
Comparison Operators & Booleans
| Operator | Meaning |
|---|---|
| == | Equal value (not recommended) |
| === | Equal value & type (USE THIS) |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater or equal |
| <= | Less or equal |
console.log(5 === "5"); // false
console.log(5 === 5); // trueBoolean values are brutally simple: true or false. No middle ground.
Logical Operators
| Operator | Meaning |
|---|---|
| && | AND |
| || | OR |
| ! | NOT (reverse) |
let age = 20;
let hasTicket = true;
if (age >= 17 && hasTicket) {
console.log("Welcome to the cinema 🍿");
}Switch Case
When conditions get messy, switch keeps things readable.
let day = "Monday";
switch (day) {
case "Monday":
console.log("Time to work 💼");
break;
case "Sunday":
console.log("Rest day 😴");
break;
default:
console.log("Just another day");
}Forget break? Everything below it runs. Chaos mode activated.
Loops: Repeat Without Crying
for loop
for (let i = 1; i <= 5; i++) {
console.log("Count:", i);
}while loop
let count = 1;
while (count <= 3) {
console.log(count);
count++;
}do while loop
let num = 1;
do {
console.log(num);
num++;
} while (num <= 3);Practice Time
Odd / Even Check
let number = 7;
if (number % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}Multiple Check
let num = 20;
if (num % 5 === 0) {
console.log("Multiple of 5");
} else {
console.log("Not a multiple of 5");
}Mini Program: Cinema Ticket
let age = 19;
let money = 50000;
if (age >= 17 && money >= 40000) {
console.log("Ticket purchased 🎬");
} else {
console.log("Requirements not met 😔");
}This is real-world logic. Programming is decision-making, not math.
Mini Challenges
- FizzBuzz (3, 5, 3 & 5)
- Loop 1–10 → Odd / Even
- Login simulation (username & password)
Try first. Don't peek. Your brain needs reps.
