TExcelTAD
Home › JavaScript › JS If Else

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:

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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}

  1. A
  2. B
  3. A and B
  4. C

Which statement is equivalent to const label = ok ? "yes" : "no";?

  1. if (ok) label = "no"; else label = "yes";
  2. let label; if (ok) { label = "yes"; } else { label = "no"; }
  3. const label = ok && "no";
  4. const label = "yes" || "no";

When does if (value) run its block?

  1. Only when value === true
  2. When value is truthy
  3. When value is defined
  4. When value is a boolean