mc0de
JavaScript · Lesson 05 of 17

Conditional Statements

Teach your code how to make decisions! Discover how JavaScript checks conditions, chooses between different actions, and responds intelligently to the information it receives.

if & else else if switch Decision Making
● ● ●   conditionals.js
const score = 85;

if (score >= 70) {
  console.log("Passed!");
} else {
  console.log("Keep trying!");
}
CONSOLE OUTPUT
> Ready to check…
01 / Getting Started

What Are Conditional Statements?

Every day, you make decisions based on conditions. If it’s raining, you bring an umbrella. If your phone battery is low, you charge it. If you finish your work early, you might relax.

JavaScript uses conditional statements to make similar decisions. They allow your program to check information and choose which instructions to run.

Your first decision
const temperature = 85;

if (temperature > 80) {
  console.log("It's a hot day!");
}
Console output
It’s a hot day!

JavaScript checks whether temperature > 80 is true. Because 85 is greater than 80, the message appears. If the condition were false, the code inside the braces would be skipped.

02 / The if Statement

Run Code Only When a Condition Is True

The if statement is the simplest way to make a decision. It checks a condition and runs a block of code when that condition is truthy.

Checking a user’s age
const age = 20;

if (age >= 18) {
  console.log("You are an adult.");
}

The condition goes inside parentheses. The instructions to run go inside curly braces.

Remember: A condition often produces true or false, but JavaScript also accepts other values and converts them to truthy or falsy for decision-making.
03 / The else Statement

Choose Between Two Outcomes

What happens when a condition isn’t met? Add an else block to provide another action.

Checking a password length
const passwordLength = 6;

if (passwordLength >= 8) {
  console.log("Password length accepted.");
} else {
  console.log("Use at least 8 characters.");
}
Console output
Use at least 8 characters.

Only one of these blocks runs. If the condition is true, JavaScript executes the if block. Otherwise, it executes the else block.

This example checks only password length. A real password-strength check would consider additional factors.

04 / The else if Statement

Handle More Than Two Possibilities

Sometimes your program needs several possible outcomes. Use else if to check additional conditions in order.

Assigning a letter grade
const score = 85;

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("Keep practicing!");
}
Console output
Grade B

JavaScript checks the conditions from top to bottom. Once it finds a matching branch, it runs that block and skips the remaining branches.

Important: Put the highest score thresholds first. If you check score >= 70 before score >= 90, a score of 95 would match the earlier condition and never reach the A-grade branch.
05 / Logical Conditions

Make Smarter Decisions

You can combine multiple conditions using the logical operators from Lesson 04.

Checking event eligibility
const age = 21;
const hasTicket = true;

if (age >= 18 && hasTicket) {
  console.log("Welcome to the event!");
} else {
  console.log("Entry requirements not met.");
}

The && operator requires both conditions to be true. The || operator allows either condition to be true, while ! reverses a Boolean value.

Checking two possible discounts
const isStudent = true;
const isSenior = false;

if (isStudent || isSenior) {
  console.log("Discount available!");
}

Combining conditions helps your programs respond to more realistic situations.

06 / The switch Statement

Choose From Several Exact Matches

When you’re comparing one value against several specific possibilities, a switch statement can make your code easier to read.

Choosing a day
const day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start the week strong!");
    break;
  case "Friday":
    console.log("Almost the weekend!");
    break;
  default:
    console.log("Make today count!");
}
Console output
Start the week strong!

Each case checks for a matching value using strict equality. The break statement stops execution from continuing into the next case. The default block runs when no case matches.

07 / The Ternary Operator

Write Simple Decisions in One Expression

For short decisions between two values, JavaScript offers the ternary operator, written with ? and :.

A quick decision
const score = 85;

const result = score >= 70
  ? "Passed!"
  : "Keep trying!";

console.log(result);
Console output
Passed!

The expression before ? is the condition. The value after ? is selected when it’s truthy; the value after : is selected otherwise.

Ternary expressions are useful for simple choices. For several branches or more complex instructions, if and else are often easier to read.

Try a live decision

Choose a score and see which grade JavaScript assigns.

Choose a score and click Check Grade.
🚀 mC0de Challenge

Build an Interactive Grade Checker

Create a webpage that accepts a student’s score and displays the corresponding letter grade along with an encouraging message.

Your Mission

  1. Create a number input where students can enter a score between 0 and 100.
  2. Add a button that checks the score.
  3. Use if, else if, and else to assign grades A, B, C, D, or F.
  4. Display the grade and a helpful message on the webpage.
  5. Show an error message when the score is missing or outside the allowed range.
  6. Try several scores to make sure every grade works.
Extra challenge: Change the result color depending on the grade. Can you make A and B green, C orange, and D or F red?
💻 Interactive JavaScript Sandbox

Build Your Grade Checker!

Edit the grading rules, customize the messages, and run your code. Then enter different scores in the live result to see your conditions in action.

Live Result
Lesson 05 Recap

What You’ve Learned

You’re Making JavaScript Smarter!

You’ve learned how to guide your programs through different decisions and outcomes.

  • Use if to run code when a condition is met.
  • Use else to provide an alternative action.
  • Handle multiple outcomes with else if.
  • Combine conditions using logical operators.
  • Use switch for several exact matches.
  • Write simple choices with the ternary operator.
  • Build a practical, interactive Grade Checker.

Next up: Loops — discover how to repeat instructions without writing the same code again and again!

Your Learning Journey

0 / 17 Completed

Keep going! Every completed lesson brings you closer to mastering JavaScript.

🎉

Nice Work!

You've learned how to make JavaScript choose between different outcomes using if, else if, else, switch, and the ternary operator. You've even built your own interactive Grade Checker. Excellent progress!