if
Runs a block when the condition is truthy.
if, else and else if let JavaScript choose a path. They are the foundation for validation, permissions, messages and user interface state.
const status = "review";
if (status === "draft") {
console.log("Keep editing.");
} else if (status === "review") {
console.log("Check the content.");
} else {
console.log("Publish the result.");
}
Control Flow
An if statement runs a block only when its condition is truthy. An else block runs when the first condition is not met. An else if block checks another condition before falling through to the final else.
This is the most common control-flow pattern in JavaScript. You use it when a program must choose between different messages, states, validation paths, rendering branches or calculations.
The professional skill is not only writing the keyword. The skill is naming conditions clearly, keeping branches small and ordering checks from specific to general.
Runs a block when the condition is truthy.
Adds another condition when the previous one did not match.
Handles the fallback path when no earlier condition matched.
Returns early when a required condition is missing, keeping the main path clean.
Examples
const score = 82;
const hasMinimumScore = score >= 70;
if (hasMinimumScore) {
console.log("Passed");
} else {
console.log("Needs more practice");
}
if (x > 69 && x !== null && !blocked) {
run();
}
// What is x? What does 69 mean? Why is blocked involved?
Rules that matter
Control flow decides which path code takes. Keep that path visible: name conditions, protect loop boundaries, avoid accidental fallthrough and use early exits only when they make the main path clearer.
const isValidScore = score >= 70 is easier to read than repeating raw comparisons.
Large branches usually need functions, not deeper indentation.
Check invalid or specific cases before broad fallback cases.
Do not add else when a guard clause already ended the path.
Use comparison in conditions and keep assignment visible on its own line.
Conditions often fail at exactly 0, 1, maximum, minimum and empty values.
Production thinking
Control flow is where requirements become behavior. A tiny condition can change what users see, what data gets saved and which action happens next.
Conditions often choose validation messages, focus behavior and disabled states. The wrong branch can leave users without clear feedback.
Production conditions should be easy to log, test and review. Named conditions and early returns prevent deeply nested logic.
If JavaScript conditions decide whether headings, links or important content appear, those branches must be reliable and not depend on fragile state.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Read each question out loud and answer it without looking at the examples. Control flow only clicks when you can trace the path in your own words.