Decisions & Loops Lesson 12 of 30
JavaScript if, else and else if
Make your program decide what to do with if, else and else if. Build a DepEd-style grade descriptor and a simple login check, and learn the ternary operator.
Programs become smart when they can choose. "If the grade is 75 or more, show PASSED; otherwise, show FAILED." In JavaScript, decisions are made with if and else.
The if statement
if (condition) {
// runs only when the condition is true
}
<p id="out">Checking…</p>
<script>
const grade = 88;
if (grade >= 75) {
document.getElementById("out").textContent = "PASSED 🎉";
}
</script>
Change the grade to 70 and run it. The message stays "Checking…" because nothing tells the program what to do when the condition is false.
else: the other path
<p id="out"></p>
<script>
const grade = 70;
let result;
if (grade >= 75) {
result = "PASSED 🎉";
} else {
result = "FAILED. Kaya mo 'yan next time! 💪";
}
document.getElementById("out").textContent = result;
</script>
else if: many choices
For more than two outcomes, chain conditions with else if. JavaScript checks them from top to bottom and runs only the first one that is true.
<input id="g" type="number" value="91" style="width:80px">
<button onclick="describe()">Describe</button>
<p id="out"></p>
<script>
function describe() {
const g = Number(document.getElementById("g").value);
let d;
if (g >= 90) {
d = "Outstanding";
} else if (g >= 85) {
d = "Very Satisfactory";
} else if (g >= 80) {
d = "Satisfactory";
} else if (g >= 75) {
d = "Fairly Satisfactory";
} else {
d = "Did Not Meet Expectations";
}
document.getElementById("out").textContent = `${g}: ${d}`;
}
describe();
</script>
Notice the order: we check the highest range first. A grade of 91 is also >= 85 and >= 80, but only the first match runs, so it correctly gets "Outstanding".
Combining conditions
<input id="u" placeholder="Username"> <input id="p" type="password" placeholder="Password">
<button onclick="login()">Log in</button>
<p id="out"></p>
<script>
function login() {
const user = document.getElementById("u").value.trim();
const pass = document.getElementById("p").value;
let msg;
if (user === "" || pass === "") {
msg = "Please fill in both boxes.";
} else if (user === "admin" && pass === "1234") {
msg = "✅ Welcome, admin!";
} else {
msg = "❌ Wrong username or password.";
}
document.getElementById("out").textContent = msg;
}
</script>
Nested if
You can put an if inside another if, but deep nesting gets hard to read. Usually && or else if is cleaner:
// Nested
if (age >= 18) {
if (hasID) {
allowVote();
}
}
// Cleaner
if (age >= 18 && hasID) {
allowVote();
}
The ternary operator
For a quick either-or value, use the ternary operator: condition ? valueIfTrue : valueIfFalse.
const status = grade >= 75 ? "PASSED" : "FAILED";
const label = count === 1 ? "1 item" : `${count} items`;
It's great for short choices. For anything longer, use a normal if…else.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04