# Conditionals: if, switch & ternary

> How to make your code think and make decisions using if statements, switch cases, and the concise ternary operator.

Subject: JavaScript | Lesson 9 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/conditionals
Previous: [null, undefined & NaN](https://codewithanimation.in/javascript/null-undefined-nan.md) | Next: [Loops: for, while, for...of, for...in](https://codewithanimation.in/javascript/loops.md)

## What are conditionals?

Conditionals are the foundation of logic in programming. They allow your code to execute different instructions based on whether a specific condition is true or false.

Without conditionals, a program would just run straight from top to bottom every single time.

## When to use which?

Use `if...else if...else` for complex logic, checking ranges (like `age > 18`), or combining multiple boolean conditions.

Use `switch` when checking a single variable against many specific, discrete values (like evaluating a `status` string).

Use the ternary operator (`? :`) for quick, single-line assignments, such as determining a button's color based on an isActive boolean.

## Syntax overview

`if (condition) { ... }`: The block runs only if the condition evaluates to a truthy value.

`switch (value) { case x: ... break; }`: The switch checks if `value === x`. It requires the `break` keyword, otherwise it will 'fall through' and execute the next case too!

`condition ? trueResult : falseResult`: The ternary operator evaluates the condition. If truthy, it returns the left side of the colon; if falsy, it returns the right side.

## Common mistake: The fall-through gotcha

A very common bug in JavaScript is forgetting the `break` statement inside a `switch` case. If you forget it, JavaScript will continue executing the code in every subsequent case below it, regardless of whether those cases match the original value!

## Tip: Ternary chaining

You can chain ternaries for if/else if logic: `score > 90 ? 'A' : score > 80 ? 'B' : 'C'`. However, use this sparingly; if it gets too long, a standard `if` statement is much easier to read.

## Try it: Branching Logic

Select a user role to see how an `if / else if`, a `switch`, and a `ternary` would evaluate it and branch the code execution.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/conditionals

## Check yourself

### Question 1

What happens if you omit the `break` statement in a `switch` case?

- A. The code 'falls through' and executes the next case.
- B. A SyntaxError is thrown.
- C. The switch block immediately exits.

Correct answer: A

Without a `break` statement, a switch case will 'fall through' and execute the code in the very next case block, even if that case's condition does not match.

- Why not B: Incorrect. The syntax is valid, it just creates a logical bug.
- Why not C: Incorrect. That is what `break` does.

### Question 2

Which statement uses strict equality (`===`) under the hood to compare values?

- A. The switch statement
- B. The if statement
- C. The ternary operator

Correct answer: A

A `switch (value)` statement uses strict equality (`value === case`) to determine which case block to execute. Type coercion does not occur.

- Why not B: Incorrect. `if` evaluates truthiness, not strict equality against a specific value.
- Why not C: Incorrect. The ternary evaluates truthiness, just like `if`.

### Question 3

What is the result of `const status = true ? 'Active' : 'Inactive';`?

- A. 'Active'
- B. 'Inactive'
- C. undefined

Correct answer: A

Because the condition is `true`, the ternary operator returns the value immediately after the `?` mark ('Active'). If it were false, it would return the value after the `:` mark.

- Why not B: Incorrect. The condition is true, so it takes the left path.
- Why not C: Incorrect. The ternary returns the string 'Active'.

## Remember this

- `if` statements evaluate whether a condition is truthy or falsy.
- `switch` statements use strict equality (`===`) and require a `break` to prevent fall-through.
- The ternary operator (`? :`) is a concise way to assign a value based on a condition.
