Learning Goals
- Write
if,else if, andelse. - Use comparison operators.
- Build simple pass/fail logic.
- Read conditions like plain English statements.
Core Logic
Use conditions to choose what your program should do in different situations.
if, else if, and else.// Save as lesson3.js
let score = 74;
if (score >= 80) {
console.log("Excellent");
} else if (score >= 50) {
console.log("Pass");
} else {
console.log("Retry");
}
node lesson3.js in your terminal to see the output.Useful operators: ===, !==, >, <, >=, <=.
When we write if (score >= 80), we mean: "only run this block if score is 80 or above."
Imagine it is morning and you are deciding whether to take a boda or walk.
let hasRain = true;
let hasMoney = false;
if (hasRain === true && hasMoney === true) {
console.log("Take boda");
} else if (hasRain === true) {
console.log("Wait for rain to reduce");
} else {
console.log("Walk to class");
}
This is exactly how decision logic works in real apps: check condition, choose action.
Conditional logic is where your program starts to feel intelligent. Without if, a program can only do one fixed path. With if, the same program can react differently depending on user input or real-world data. This is the same idea behind pass/fail checks, login rules, form validation, and recommendation systems.
As a beginner, read each condition like spoken language before you run the code. For example: "If marks are greater than or equal to 80, print B." When your brain can read code as clear English, writing logic becomes much easier and your bugs reduce fast.
= instead of comparison ===.{ } around an if block.Create a grading script with 5 grade bands: A, B, C, D, F.
let marks = 82;
if (marks >= 90) {
console.log("A");
} else if (marks >= 80) {
console.log("B");
} else if (marks >= 70) {
console.log("C");
} else if (marks >= 50) {
console.log("D");
} else {
console.log("F");
}
console.log before the if block if your result looks wrong.