Should the user see this page or get redirected? Is the password correct or not? Does the cart have items or is it empty?
Every one of those decisions is handled by an if/else statement. It is one of the most important concepts in all of coding — and one of the simplest to understand once you see it in action.
What Is an If/Else Statement?
Real-Life Analogy — Traffic Lights
Think of a traffic light. If the light is green — you go. If it is not green — you stop. No maybe. Just true or false — and a clear action for each.
How If/Else Works Step by Step
if (light === “green”) {
console.log(“Go!”); // ✓ runs this
} else {
console.log(“Stop!”); // skipped
}
The Basic If Statement
The simplest form of decision making in code.
The if statement runs a block of code only when a condition is true. If the condition is false — nothing happens and the program moves on.
Syntax in JavaScript
// code runs only if condition is true
}
if (age >= 18) {
console.log(“Access granted”); // ✓ runs — age is 20
}
Syntax in Python
Python uses a colon and indentation instead of curly braces — no brackets around the condition either.
if age >= 18:
print(“Access granted”) # ✓ runs — age is 20
( ) around conditionUses
{ } for the block( ) neededUses
: + indentationWhat Happens When Condition is False?
With just an if and no else — nothing happens when the condition is false. The program skips that block and continues to the next line.
If/Else — Two Paths
A basic if only runs when true. But what about false? The else block catches that. Together they give your program exactly two paths — one for true, one for false.
Adding the Else Block
The else block runs when the if condition is false. You never need to write another condition — it just catches everything the if did not.
runs this ✓
runs this ✓
// runs when TRUE
} else {
// runs when FALSE
}
Real Code Example
A simple login check — either the password is correct or it is not. No third option.
if (password === “abc123”) {
console.log(“Welcome! You are logged in.”);
} else {
console.log(“Wrong password. Try again.”);
}
if password == “abc123”:
print(“Welcome! You are logged in.”)
else:
print(“Wrong password. Try again.”)
Nested If/Else — Conditions Inside Conditions
Check something — then check something else inside that.
Sometimes one condition is not enough. You need to check something — and then check something else inside that. That is called a nested if — an if statement inside another if statement.
When to Use Nested If
Use nested if when the second check only makes sense if the first one already passed.
Simple Nested Example
let hasLicense = true;
if (age >= 18) {
// first check passed — now check license
if (hasLicense === true) {
console.log(“You can drive!”);
} else {
console.log(“You need a license first.”);
}
} else {
console.log(“You are too young to drive.”);
}
Else If — Checking Multiple Conditions
Chain multiple conditions together — one after another.
Sometimes two paths are not enough. What if you need three, four, or five different outcomes? That is where else if comes in — it lets you chain multiple conditions together.
Using else if in JavaScript
The program checks each condition from top to bottom. The first one that is true wins — everything else is skipped.
// runs if first is true
} else if (second condition) {
// runs if second is true
} else {
// runs if nothing matched
}
Using elif in Python
Python uses elif instead of else if — but it works exactly the same way.
# runs if first is true
elif second_condition:
# runs if second is true
else:
# runs if nothing matched
Real-Life Grade Checker Example
if (score >= 90) {
console.log(“A — Excellent!”);
} else if (score >= 75) {
console.log(“B — Good job!”); // ✓ this runs
} else if (score >= 60) {
console.log(“C — Passing”);
} else {
console.log(“F — Please retry”);
}
The Ternary Operator — One-Line If/Else
A shortcut for simple if/else — no curly braces needed.
Sometimes your if/else is simple enough to write in a single line. The ternary operator lets you do exactly that — no curly braces, no extra lines.
Ternary Syntax
The ternary operator has three parts — that is why it is called “ternary.”
let age = 20;
if (age >= 18) {
console.log(“Adult”);
} else {
console.log(“Minor”);
}
console.log(status);
When to Use It
Real-Life If/Else Examples
Three examples you will actually build as a beginner.
Age Checker
if (age >= 18) {
console.log(“Access granted”);
} else {
console.log(“You must be 18 or older”);
}
Login Validator
let password = “pass123”;
if (username === “sara” && password === “pass123”) {
console.log(“Welcome back, Sara!”);
} else {
console.log(“Invalid credentials”);
}
Shopping Discount System
if (cartTotal >= 200) {
console.log(“20% discount applied!”);
} else if (cartTotal >= 100) {
console.log(“10% discount applied!”); // ✓ runs
} else {
console.log(“No discount — spend more!”);
}
FAQs About If/Else
Quick answers to the questions beginners ask most
What is the difference between if and else if?
if starts a new condition check — it always runs first. else if only runs when the previous condition was false. You can have one if — but as many else if blocks as you need after it.if as the first door and else if as extra doors — only opened if the ones before them were locked.Can you have an if without an else?
else block is completely optional. If nothing needs to happen when the condition is false — just write the if by itself.showDashboard();
}
// no else needed
showDashboard();
} else {
showLogin();
}
else only when you actually need something to happen in the false case. Don’t add it just to have it.What is the difference between elif and else if?
if (x > 10) { … } else if (x > 5) { … }
# Python
if x > 10: … elif x > 5: …
