JavaScript If Else
Conditional statements let your program make decisions: run one block of code when a condition is true, and a different one (or nothing) when it's false.
if
The block after if runs only when the condition in parentheses is truthy:
const temperature = 32;
if (temperature > 30) {
console.log("It's a hot day — stay hydrated!");
}
console.log("Weather check finished.");It's a hot day — stay hydrated!
Weather check finished.
else
else provides the alternative block that runs when the condition is falsy:
const balance = 40;
const price = 55;
if (balance >= price) {
console.log("Purchase complete.");
} else {
console.log(`Not enough money: you need ${price - balance} more.`);
}Not enough money: you need 15 more.
else if
Chain several conditions with else if. They're checked from top to bottom, and only the first matching block runs — so order them from most specific to least:
function grade(score) {
if (score >= 90) {
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70) {
return "C";
} else if (score >= 60) {
return "D";
} else {
return "F";
}
}
for (const s of [95, 83, 70, 61, 12]) {
console.log(s, grade(s));
}95 A
83 B
70 C
61 D
12 F
Combining conditions
Use && (and), || (or) and ! (not) to build more complex conditions. Parentheses make the intent clear:
const user = { age: 17, hasConsent: true, banned: false };
if (!user.banned && (user.age >= 18 || user.hasConsent)) {
console.log("Access granted");
} else {
console.log("Access denied");
}Access granted
Guard clauses and early returns
Deeply nested ifs are hard to read. Inside functions, handle the special cases first and return early — the main logic then stays unindented:
function shippingCost(order) {
if (!order) return "no order";
if (order.items === 0) return "empty cart";
if (order.total >= 50) return 0;
return order.express ? 9.99 : 4.99;
}
console.log(shippingCost(null));
console.log(shippingCost({ items: 0, total: 0 }));
console.log(shippingCost({ items: 3, total: 80 }));
console.log(shippingCost({ items: 1, total: 20, express: true }));no order
empty cart
0
9.99
For a one-statement body you may leave out the braces (if (x) return;), but always use braces for anything longer — it avoids bugs when lines are added later.
The conditional (ternary) operator
When you're choosing between two values, the ternary operator condition ? a : b is more compact than if…else:
const items = 1;
console.log(`You have ${items} ${items === 1 ? "item" : "items"} in your cart.`);
const hour = 20;
const greeting = hour < 12 ? "Good morning" : hour < 18 ? "Good afternoon" : "Good evening";
console.log(greeting);You have 1 item in your cart.
Good evening
Tip: Nesting ternaries more than one level deep hurts readability. Switch to
if…else(or a lookup object) when it gets complicated.
Test yourself
With const n = 85, which block runs? if (n > 50) {A} else if (n > 80) {B} else {C}
- A
- B
- A and B
- C
Which statement is equivalent to const label = ok ? "yes" : "no";?
if (ok) label = "no"; else label = "yes";let label; if (ok) { label = "yes"; } else { label = "no"; }const label = ok && "no";const label = "yes" || "no";
When does if (value) run its block?
- Only when value === true
- When value is truthy
- When value is defined
- When value is a boolean