Logo LIBR
Halo

Portfolio

Back to Blog
8 min read
JS Module 2Jun 2026

Logic Brain Gym: Mastering Control Flow

JavaScript, Logic, Control Flow
Syifa Fauziyah Arizal

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?

JavaScript
if (condition) {
  // runs if true
} else {
  // runs if false
}

Real-life example:

JavaScript
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

OperatorMeaning
==Equal value (not recommended)
===Equal value & type (USE THIS)
!=Not equal
>Greater than
<Less than
>=Greater or equal
<=Less or equal
JavaScript
console.log(5 === "5"); // false
console.log(5 === 5);   // true

Boolean values are brutally simple: true or false. No middle ground.

Logical Operators

OperatorMeaning
&&AND
||OR
!NOT (reverse)
JavaScript
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.

JavaScript
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

JavaScript
for (let i = 1; i <= 5; i++) {
  console.log("Count:", i);
}

while loop

JavaScript
let count = 1;

while (count <= 3) {
  console.log(count);
  count++;
}

do while loop

JavaScript
let num = 1;

do {
  console.log(num);
  num++;
} while (num <= 3);

Practice Time

Odd / Even Check

JavaScript
let number = 7;

if (number % 2 === 0) {
  console.log("Even");
} else {
  console.log("Odd");
}

Multiple Check

JavaScript
let num = 20;

if (num % 5 === 0) {
  console.log("Multiple of 5");
} else {
  console.log("Not a multiple of 5");
}

Mini Program: Cinema Ticket

JavaScript
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.

Key Takeaways

  • Logic is the heart of JavaScript
  • if–else is the foundation
  • Operators must be memorized
  • Loops save time and sanity
  • Mini programs train thinking, not typing

More to read