Lesson 2: if, else if, and else Statements

Learning Objectives

By the end of this lesson, learners will:

  • Understand how to make decisions in JavaScript using if, else if, and else
  • Learn the correct syntax for conditional statements
  • Create programs that react to different conditions
  • Understand the importance of using comparison operators inside conditions
  • Apply logic to real-world decision-making examples

Why Use if Statements?

Sometimes, you only want to run a piece of code if a certain condition is true.

For example:

  • Show a special message if a user is logged in
  • Charge extra if shipping is international
  • Display “Good morning” if it’s before noon

This is where the if, else if, and else statements come into play.


1. Basic if Statement

Syntax:

if (condition) {
  // code to run if condition is true
}

Example:

let age = 18;

if (age >= 18) {
  console.log("You can vote!");
}

In this example:

  • age >= 18 is the condition
  • "You can vote!" is only printed if the condition is true

2. else Statement

The else block runs if the condition is false.

Syntax:

if (condition) {
  // run if true
} else {
  // run if false
}

Example:

let isRaining = false;

if (isRaining) {
  console.log("Take an umbrella.");
} else {
  console.log("Enjoy the sunshine!");
}

🧠 Only one of the blocks will run, never both.


3. else if Statement

Use else if to check multiple conditions, one after the other.

Syntax:

if (condition1) {
  // run if condition1 is true
} else if (condition2) {
  // run if condition2 is true
} else {
  // run if none are true
}

Example:

let score = 72;

if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 80) {
  console.log("Grade: B");
} else if (score >= 70) {
  console.log("Grade: C");
} else {
  console.log("Grade: F");
}

🧠 JavaScript checks each condition in order, top to bottom, and runs the first one that is true.


Important Syntax Rules

  • Use parentheses () for the condition
  • Use curly braces {} to define the code block
  • You can nest if statements, but keep it readable

Real-Life Example

Clothing Suggestion App:

let temperature = 15;

if (temperature > 25) {
  console.log("Wear a t-shirt.");
} else if (temperature > 15) {
  console.log("Wear a light jacket.");
} else {
  console.log("Wear a coat.");
}

This shows how JavaScript can adapt based on real-world data.


Practice Activity

Try this in your browser console:

let time = 20;

if (time < 12) {
  console.log("Good morning");
} else if (time < 18) {
  console.log("Good afternoon");
} else {
  console.log("Good evening");
}

🧠 Output: "Good evening"


Logical Operators in Conditions

You can combine multiple conditions using:

OperatorMeaningExample
&&ANDage >= 18 && age < 30
``
!NOT (negation)!isLoggedIn

Key Takeaways

  • if checks a condition — code runs only if it’s true
  • else handles the opposite — runs when the if condition is false
  • else if allows for multiple possible outcomes
  • Use logical operators for more complex decisions