Skip to content
BasicsChapter 10 / 47Beginner5 min read

Conditionals: if, switch, ternary

Learn to make decisions in JavaScript with if else, switch and the ternary operator, plus clean patterns like early return and object lookup.

On this page

1. What are conditionals?

JavaScript has three main tools for decisions:

ToolWhen to use it
if / else if / elseGeneral conditions, ranges, multiple checks
switchMatching one value against several fixed options
Ternary ? :Picking one of two values, in one line

2. Why does it matter?

Every app has to make decisions. Is the user logged in or not, is the cart empty or not, what is the order status. Without conditionals, a program would do the same thing every time.

Messy conditionalsClean conditionals
An if inside an if inside an ifA direct flow with early return
A long else if chain for fixed valuesswitch or object lookup
A full if else for a small valueA ternary in one line
The condition is hard to understandThe condition held in a well named variable

3. Where is it used?

Real useTool
Login check, redirectif + early return
Grade from marksif / else if (ranges)
Message for an order statusswitch or object lookup
Button text "Follow" or "Unfollow"Ternary
Form validationif checks

4. How does it work?

if turns the condition into a boolean and checks it. The block of the first true condition runs, and the rest are skipped.

Flow of if / else if / else
Check condition 1
If true, run block 1 and exit
Otherwise check condition 2
If none is true, the else block

switch compares the value with each case using === (strict equality). When it finds a match, the code runs until a break.

5. Code example

if / else if / else for ranges:

js
function getGrade(marks) {
  if (marks >= 80) {
    return "A";
  } else if (marks >= 65) {
    return "B";
  } else if (marks >= 50) {
    return "C";
  }
  return "Fail";
}

console.log(getGrade(72));

Output: B.

switch and object lookup:

js
function statusMessage(status) {
  switch (status) {
    case "pending":
      return "Order received";
    case "shipped":
      return "Order is on the way";
    case "delivered":
      return "Order delivered";
    default:
      return "Unknown status";
  }
}

const messages = {
  pending: "Order received",
  shipped: "Order is on the way",
};

console.log(statusMessage("shipped"));
console.log(messages["pending"] ?? "Unknown status");

Ternary and early return:

js
const isFollowing = true;
const label = isFollowing ? "Unfollow" : "Follow";

function checkout(user, cart) {
  if (!user) return "Please log in first";
  if (cart.length === 0) return "Your cart is empty";
  return "Go to the payment page";
}

console.log(label);
console.log(checkout({ name: "Asad" }, []));

Output: Unfollow, Your cart is empty.

How does the code work?

ItemMeaning
else ifIf the condition above is false, check the next one
case "pending":If status === "pending", start running from here
return / breakLeaves the switch, otherwise it falls into the next case
defaultWhen no case matches
messages[status]Object lookup, a short alternative to switch
a ? b : cb if a is true, otherwise c
if (!user) returnEarly return (guard clause), less nesting

Interview tip

Forgetting break in a switch is called fall through. Interviews often ask what the output will be. Remember: after a match, all the cases below keep running until a break or return.

6. Common mistakes

Mistake: Forgetting break in a switch

Why? After a match, the cases below also run, giving a wrong message or doing the work twice.

Better: Write break or return at the end of every case.

Mistake: Writing a nested ternary

Why? Reading and debugging several ternaries in one line is very hard.

Better: With more than two options, use if else, switch or object lookup.

Mistake: Writing = inside a condition

Why? if (role = 'admin') assigns the value and is always true, so every user becomes an admin.

Better: Write === for comparison. = is only for assigning a value.

Mistake: Deep nesting

Why? An if inside an if inside an if pushes the code to the right and hides the logic.

Better: Get the bad cases out first with an early return, and write the main logic straight at the end.

7. What to learn before and after

Final quick revision

PointDetail
if / elseGeneral conditions and ranges
switchFixed values, matched with ===, break is needed
TernaryOne of two values, in one line
Object lookupMapping a value to a message or config
Early returnLess nesting, cleaner code