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.
const score = 85;
if (score >= 70) {
console.log("Passed!");
} else {
console.log("Keep trying!");
}
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.
const temperature = 85;
if (temperature > 80) {
console.log("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.
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.
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.
true or false, but JavaScript also accepts other values and converts them to truthy or falsy for decision-making.Choose Between Two Outcomes
What happens when a condition isn’t met? Add an else block to provide another action.
const passwordLength = 6;
if (passwordLength >= 8) {
console.log("Password length accepted.");
} else {
console.log("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.
Handle More Than Two Possibilities
Sometimes your program needs several possible outcomes. Use else if to check additional conditions in order.
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!");
}
JavaScript checks the conditions from top to bottom. Once it finds a matching branch, it runs that block and skips the remaining branches.
score >= 70 before score >= 90, a score of 95 would match the earlier condition and never reach the A-grade branch.Make Smarter Decisions
You can combine multiple conditions using the logical operators from Lesson 04.
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.
const isStudent = true;
const isSenior = false;
if (isStudent || isSenior) {
console.log("Discount available!");
}
Combining conditions helps your programs respond to more realistic situations.
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.
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!");
}
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.
Write Simple Decisions in One Expression
For short decisions between two values, JavaScript offers the ternary operator, written with ? and :.
const score = 85;
const result = score >= 70
? "Passed!"
: "Keep trying!";
console.log(result);
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.
Choose a score and see which grade JavaScript assigns.
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
- Create a number input where students can enter a score between 0 and 100.
- Add a button that checks the score.
- Use
if,else if, andelseto assign grades A, B, C, D, or F. - Display the grade and a helpful message on the webpage.
- Show an error message when the score is missing or outside the allowed range.
- Try several scores to make sure every grade works.
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.
What You’ve Learned
You’re Making JavaScript Smarter!
You’ve learned how to guide your programs through different decisions and outcomes.
- Use
ifto run code when a condition is met. - Use
elseto provide an alternative action. - Handle multiple outcomes with
else if. - Combine conditions using logical operators.
- Use
switchfor 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 CompletedKeep 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!


