Chapter V · Control Flow
The if Statement
The if statement is how JavaScript makes decisions — it runs a block of code only when a condition is true, and skips it otherwise.
01 What Is the if Statement?
An if statement is a control-flow statement — a piece of syntax that changes the order in which code runs, instead of letting it execute strictly top to bottom. It takes a condition — an expression in parentheses — and runs the block that follows only when that condition is truthy (a value JavaScript treats as true when it needs a boolean).
Almost every program needs this. Without it, code could only ever do the same fixed sequence of steps regardless of input — an if lets the program react to data instead of ignoring it.
02 Simple Example
A block that only logs when a score clears a threshold.
const score = 85;\n\nif (score >= 60) {\n console.log("Passed exam!");\n}
If score had been 40, the condition 40 >= 60 would evaluate to false, and the block would simply be skipped — nothing would print, and no error would occur.
03 How It Works
The engine evaluates the expression inside the parentheses first, on its own, before looking at the block at all. That value — whatever it is — is then coerced to a boolean using the same rules as truthy/falsy conversion. Only a truthy result lets the block run.
The condition is always resolved to true or false before the engine decides whether to enter the block.
04 Step-by-Step
The engine evaluates the expression inside the if parentheses.
The result is converted to a boolean using truthy/falsy rules if it wasn't one already.
If the result is true, the block runs top to bottom.
If the result is false, the block is skipped entirely — execution resumes at the first statement after it.
05 Common Mistakes & Edge Cases
Assignment instead of comparison. Writing if (x = 5) assigns 5 to x and then checks whether 5 is truthy — it is, so the block always runs, silently overwriting x. Use === for comparisons.
Omitting braces. if (x) doThing(); is legal — the block can be a single statement without braces — but adding a second line without braces means only the first line is conditional, a common source of bugs when code is edited later. Most style guides require braces for exactly this reason.
Note: An if is a statement, not an expression — it produces no value and cannot be used inside a template literal or as a function argument. The ternary operator exists for that case.
06 Mental Model
"A locked door that only opens for a truthy key."
The engine tries the condition like a key in a lock. If it fits (truthy), the door opens and the block runs. If it doesn't, the door stays shut and execution walks straight past it.
07 Interview Questions
Q: Does an if block create a new scope?
Yes for let/const — they're block-scoped to the {} — but var declared inside still attaches to the nearest function or global scope, ignoring the block.
Q: What counts as falsy in an if condition?
false, 0, -0, 0n, "", null, undefined, and NaN — everything else, including "0" and [], is truthy.
Key Takeaway
if evaluates a condition once, coerces it to a boolean, and runs its block only when that result is true — everything else in conditional logic builds on this one behavior.