Two Outcomes
if...else lets you define both the true branch and the false branch.
This is the most common pattern for binary decisions.
JavaScript Tutorial
The if...else statement provides two clear paths: one for true and one for false.
It is the most common conditional structure in day-to-day JavaScript.
Most decisions are binary: success or failure, logged in or logged out.
if...else keeps both outcomes explicit, making logic easier to maintain.
if (condition) { ... } else { ... }const isMember = true;
if (isMember) {
console.log("Member price");
} else {
console.log("Standard price");
}Two branches for two outcomes.
const items = [];
if (items.length > 0) {
console.log("Show list");
} else {
console.log("Show empty state");
}if...else is common for UI rendering decisions.
if...else lets you define both the true branch and the false branch.
This is the most common pattern for binary decisions.
Use if...else for login states, toggles, and empty vs populated UI states.
It keeps your logic explicit and predictable.
Large if/else blocks are hard to test. Extract logic into functions when needed.
Small branches are easier to read and maintain.
const role = "editor";
if (role === "admin") {
console.log("All access");
} else {
console.log("Limited access");
}Handle a yes/no permission check.
const name = "";
if (name) {
console.log("Hello", name);
} else {
console.log("Hello, guest");
}Empty strings are falsy, so be explicit if needed.
Without
let message;
if (isLoggedIn) {
message = "Welcome";
} else {
message = "Please sign in";
}With
const message = isLoggedIn ? "Welcome" : "Please sign in";Consider guard clauses to reduce nesting.
Use else if ladder or switch for multiple branches.
Check for empty strings or zero explicitly if needed.
Extract repeated logic into helper functions.
It selects between two branches based on a condition.
When it reduces readability; prefer guard clauses or switch.
No, use it only when you need a fallback branch.
Practice: Show a free-shipping message if total is over 100, otherwise show the shipping fee.
const total = 75;
// TODO: log the correct message
One Possible Solution
const total = 75;
if (total > 100) {
console.log("Free shipping");
} else {
console.log("Shipping fee applies");
}Use it for two mutually exclusive outcomes.
Use ternary for simple expressions and if...else for larger blocks.
Yes, but deep nesting hurts readability.
Modify the condition to see which branch runs.