CodingBanana
CodingBanana
JavaScript

Conditionals

7 min read📖 Beginner
Conditionals let your code make decisions. They run different blocks of code depending on whether a condition is true or false.

Every interesting program needs to make decisions — show a welcome message if logged in, display an error if a form is incomplete, apply a discount if a user qualifies. That's what conditionals are for.

if / else

The simplest conditional runs code only if a condition is true:

const temperature = 28;

if (temperature > 25) {
  console.log("It's hot outside!");
}

Add an else block to handle the false case:

const temperature = 15;

if (temperature > 25) {
  console.log("It's hot outside!");
} else {
  console.log("Nice and cool today.");
}

else if

Chain multiple conditions with else if:

const score = 72;

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

JavaScript checks each condition from top to bottom and stops at the first one that is true.

Truthy and Falsy

In JavaScript, every value is either truthy or falsy — not just booleans:

// These are all FALSY (treated as false in conditions):
false
0
""           // empty string
null
undefined
NaN

// Everything else is TRUTHY, including:
true
1
"hello"
[]           // empty array
{}

This means you can check if a value exists directly:

const username = "Alice";

if (username) {
  console.log("Welcome, " + username + "!");
} else {
  console.log("Please enter a username.");
}
💡

Be careful with 0 and "" — they're falsy even though they're valid values. If you need to check whether a variable was assigned at all, check value !== undefined or value !== null instead.

The Ternary Operator

The ternary operator is a shorthand for simple if/else. It fits on one line:

// condition ? valueIfTrue : valueIfFalse
const age = 20;
const status = age >= 18 ? "adult" : "minor";

console.log(status);   // "adult"

Great for assigning a value based on a condition:

const isLoggedIn = true;
const message = isLoggedIn ? "Welcome back!" : "Please log in.";
console.log(message);

switch

When comparing one value against many options, switch is cleaner than a long chain of else if:

const day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start of the week!");
    break;
  case "Friday":
    console.log("Almost the weekend!");
    break;
  case "Saturday":
  case "Sunday":
    console.log("It's the weekend!");
    break;
  default:
    console.log("Midweek grind.");
}

The break statement stops the switch from falling through to the next case. The default runs if no case matches.

Try It Yourself

Next up: instead of writing the same code over and over, use loops to repeat actions automatically.

Learn about Loops →

Have anything to say about this lesson?

Your feedback helps improve these tutorials. If something was confusing or missing, let us know.

We don't currently reply to feedback — but if we add that feature in the future, we'll reach out to you.