<htmlstart/>

JS Conditions

Make decisions in code with if/else, else if, and switch.

if / else

const age = 20;

if (age >= 18) {
  console.log("You can vote.");
} else {
  console.log("Too young to vote.");
}

else if

const score = 75;

if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 70) {
  console.log("Grade: B");
} else if (score >= 50) {
  console.log("Grade: C");
} else {
  console.log("Grade: F");
}

switch

Use switch when comparing one value against many fixed options:

const day = "Monday";

switch (day) {
  case "Monday":
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
  case "Friday":
    console.log("Weekday");
    break;
  case "Saturday":
  case "Sunday":
    console.log("Weekend");
    break;
  default:
    console.log("Unknown day");
}

Truthy and Falsy

These values are falsy — they behave like false inside an if:

false, 0, "", null, undefined, NaN

// Everything else is truthy — including "0", [], {}
if ("") { /* never runs */ }
if ("hello") { /* always runs */ }
if (0) { /* never runs */ }
if ([]) { /* always runs — empty array is truthy! */ }
Don't write if (x === true) — just write if (x). Similarly, if (!x) covers null, undefined, empty string, and 0 all at once.
🧩

Test Yourself

4 questions
Q1.Which of these values is falsy in JavaScript?
Q2.What keyword prevents fall-through in a switch statement?
Q3.What does else if allow you to do?
Q4.Which is cleaner for checking one value against 5+ fixed options?