Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Conditionals

Conditionals let your program choose different paths based on a condition.

They power decision making in forms, UI flows, validation, and business rules.

Why We Need It

Every real app needs branching logic. Without conditionals, code cannot respond to user input or data.

Understanding conditionals keeps your logic predictable and easy to maintain.

Syntax

if (condition) { ... }
if (condition) { ... } else { ... }
if (a) { ... } else if (b) { ... } else { ... }
switch (value) { case x: ...; break; default: ... }

Basic Example

1. Simple if

const loggedIn = true;

if (loggedIn) {
  console.log("Welcome back");
}

Run a block only when the condition is true.

Real World Example

2. if...else

const isMember = false;

if (isMember) {
  console.log("Member price");
} else {
  console.log("Standard price");
}

Provide a fallback branch for the false case.

Multiple Use Cases

Making Decisions

Conditionals control which code runs based on a boolean expression. They are the core of business rules and UI states.

Use if/else for flexible logic and switch when you have a single value with multiple discrete cases.

  • Use `if` for a single branch.
  • Use `if...else` for two outcomes.
  • Use `else if` ladders for multiple ranges.

Readable Rules

Readable conditions reduce bugs. Prefer small, named boolean variables over long, nested expressions.

Parentheses can improve clarity when mixing logical operators.

Switch for Discrete Values

Switch statements are ideal for mapping a single value to many outcomes.

Remember to use `break` to avoid fall-through unless that is your intent.

Truthy and Falsy

JavaScript treats some values as falsy: 0, "", null, undefined, NaN, and false.

Be explicit when a value like 0 or an empty string is meaningful.

More Examples

3. else if ladder

const score = 82;

let grade;
if (score >= 90) {
  grade = "A";
} else if (score >= 80) {
  grade = "B";
} else if (score >= 70) {
  grade = "C";
} else {
...

Use else-if ladders for ranges or tiers.

4. switch statement

const role = "admin";

switch (role) {
  case "admin":
    console.log("All access");
    break;
  case "editor":
    console.log("Write access");
    break;
  default:
...

Switch maps a single value to multiple outcomes.

Comparison

Without

let label;
if (status === "paid") {
  label = "Paid";
} else {
  label = "Pending";
}

With

const label = status === "paid" ? "Paid" : "Pending";

Common Mistakes and Fixes

Forgetting else branches

Add an else when you need a clear fallback path.

Overly complex conditions

Split into named boolean variables for readability.

Missing break in switch

Add break to avoid accidental fall-through.

Relying on truthy/falsy unintentionally

Use explicit comparisons when values like 0 or "" are valid.

Interview Questions

What is the difference between if and switch?

If is flexible for many conditions; switch compares one value against cases.

What is fall-through in switch?

When a case runs into the next because break is missing.

Why avoid complex conditions?

They reduce readability and are harder to debug.

Practice Problem

Practice: Build a status label using if/else based on a payment status string.

const status = "processing";
// TODO: print a label for paid, failed, or pending

One Possible Solution

const status = "processing";
let label;
if (status === "paid") {
  label = "Paid";
} else if (status === "failed") {
  label = "Failed";
} else {
  label = "Pending";
}
console.log(label);

Frequently Asked Questions

When should I use switch?

Use switch when you compare a single value against many discrete cases.

What is an else-if ladder?

A chain of if/else-if blocks for multiple conditions.

Do I always need else?

No, only when you want a fallback action.

What are falsy values?

0, "", null, undefined, NaN, and false are falsy in JavaScript.

Try It Yourself

Try the conditional examples and change the inputs to see different branches.