Learning Objectives
- Branch code based on conditions using
if / else if / else. - Use comparison operators (
===,!==,>,<, etc.) and logical operators (&&,||,!) correctly. - Handle multiple branches with the
switchstatement. - Handle repetitive work with
forandwhileloops. - Control loop flow with
breakandcontinue.
1. Conditionals: if / else if / else
const score = 85;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 80) {
console.log("Grade B"); // ← this runs
} else if (score >= 70) {
console.log("Grade C");
} else if (score >= 60) {
console.log("Grade D");
} else {
console.log("Grade F");
}
Ternary Operator (Handling Simple Conditions)
const age = 20;
const status = age >= 19 ? "adult" : "minor";
console.log(status); // "adult"
2. Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== |
Strict equality (value + type) | 5 === "5" |
false |
!== |
Strict inequality | 5 !== "5" |
true |
== |
Loose equality (type coercion) | 5 == "5" |
true |
> |
Greater than | 10 > 5 |
true |
< |
Less than | 10 < 5 |
false |
>= |
Greater than or equal to | 5 >= 5 |
true |
<= |
Less than or equal to | 4 <= 5 |
true |
Note: Always use
===instead of==. It also compares types, so it is safer.
3. Logical Operators
// && (AND): true only when both are true
const isAdult = true;
const hasTicket = true;
console.log(isAdult && hasTicket); // true
// || (OR): true when at least one is true
const isWeekend = false;
const isHoliday = true;
console.log(isWeekend || isHoliday); // true
// ! (NOT): true → false, false → true
const isLoggedIn = false;
console.log(!isLoggedIn); // true
Short-circuit Evaluation
// && : if the left side is false, the right side is not evaluated
false && console.log("not executed");
// || : if the left side is true, the right side is not evaluated
true || console.log("not executed");
// Default value pattern (||)
const username = "";
const displayName = username || "Guest"; // "Guest"
// Optional chaining pattern (&&)
const user = { name: "John Doe" };
const name = user && user.name; // "John Doe"
4. switch Statement
It finds the matching value among several values and runs it.
const day = "Mon";
switch (day) {
case "Mon":
case "Tue":
case "Wed":
case "Thu":
case "Fri":
console.log("It is a weekday.");
break;
case "Sat":
case "Sun":
console.log("It is the weekend.");
break;
default:
console.log("Invalid input.");
}
Note: If you leave out
breakin eachcase, execution falls through to the nextcase(fall-through).
5. for Loop
// Basic for statement
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}
// Iterating over an array
const fruits = ["apple", "banana", "strawberry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
// for...of (iterating over values, ES6+)
for (const fruit of fruits) {
console.log(fruit);
}
6. while Loop
let count = 0;
while (count < 5) {
console.log(count); // 0, 1, 2, 3, 4
count++;
}
// do...while: runs at least once before the condition is checked
let num = 0;
do {
console.log(num); // 0
num++;
} while (num < 0); // runs once even though the condition is false
7. break / continue
// break: exits the loop entirely
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i); // 0, 1, 2, 3, 4
}
// continue: skips only the current iteration
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) continue; // skip even numbers
console.log(i); // 1, 3, 5, 7, 9
}
Practice Files
- Example
- Assignment
- Assignment Solution