Lesson 3 of 10

Core Logic

Lesson 3: If Statements

Use conditions to choose what your program should do in different situations.

Learning Goals

Example

// Save as lesson3.js
let score = 74;

if (score >= 80) {
  console.log("Excellent");
} else if (score >= 50) {
  console.log("Pass");
} else {
  console.log("Retry");
}
Tip: Run 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."

Real Example: Transport Decision

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.

Deeper Explanation

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.

Common Beginner Mistakes

  1. Using one equals sign: = instead of comparison ===.
  2. Forgetting braces { } around an if block.
  3. Writing conditions that can never be true.

Practice

  1. Set score to 30, 65, and 90. Check output each time.
  2. Create an age check: under 18 or 18 and above.
  3. Create a weather check: rain or no rain.
  4. Create a money check: enough fare or not enough fare.

Homework

Create a grading script with 5 grade bands: A, B, C, D, F.

Answer Guide
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");
}
Debug habit: print your variable with console.log before the if block if your result looks wrong.
← Previous Lesson Next Lesson →