JavaScript If Else
Logical operators gave you a yes/no answer. This lesson asks the next question: which lines should run? When the answer is yes, run one block. When it is no, run another. That choice is if / else / else if. You already know how to build a boolean; here you use it to pick a path.
What does if do?
if looks at a condition in parentheses. If that value is truthy, JavaScript runs the block in curly braces right after it. If the value is falsy, that block is skipped. else is the backup path for when the if failed. else if adds another check before you fall through to a final else.
The keywords at a glance
📊 Everyday if / else pieces
| Keyword | Everyday reading | Typical use |
|---|---|---|
if |
if this passes, run this block | first check |
else if |
else, if this other check passes… | middle paths |
else |
otherwise, run this | final fallback |
Keep the condition honest: a comparison (age >= 18), a logical join (age >= 18 && score >= 60), or a clear boolean variable. Then put the work of that path inside { … }.
let age = 20;
if (age >= 18) {
console.log("adult");
}
console.log("done");
if runs only when the check passes
Here age is 20, so age >= 18 is true and the Console prints adult, then done. Change age to 16 and only done remains — the if block was skipped.
The line after the closing } always runs (unless you leave the function later). if does not stop the whole program; it only decides whether its own block runs.
let age = 16;
if (age >= 18) {
console.log("adult");
} else {
console.log("under 18");
}
else covers the other path
Now the check fails, so JavaScript skips the first block and runs else. You see under 18. Exactly one of the two branches runs — never both, never neither, for a plain if / else pair.
let score = 75;
if (score >= 90) {
console.log("A");
} else if (score >= 60) {
console.log("pass");
} else {
console.log("retry");
}
else if adds middle paths
score is 75. The first check (>= 90) fails. The second (>= 60) passes, so you see pass and the final else is skipped. Order matters: put the stricter checks first when bands overlap.
If you wrote three separate if statements instead, more than one block could run. else if keeps the paths exclusive — after one match, the chain stops.
let age = 20;
let score = 75;
if (age >= 18 && score >= 60) {
console.log("allowed");
} else {
console.log("blocked");
}
let ready = false;
if (!ready) {
console.log("wait");
}
Put real checks in the parentheses
The condition can be a comparison, a logical expression, or a flipped boolean. age >= 18 && score >= 60 is true, so you see allowed. !ready is true, so you also see wait. Build the yes/no first; then let if choose the branch.
let age = 20;
let label;
if (age >= 18) {
label = "adult";
} else {
label = "under 18";
}
console.log(label);
Name the result of the branch
Sometimes you want a string (or number) from the choice, not only a console.log. Set label inside each branch, then use it once afterward. Here label becomes "adult".
Show the chosen path on the page
Compute a short message with if / else, then write it into the page.
<!DOCTYPE html>
<html>
<head>
<title>If else — page branch</title>
</head>
<body>
<p id="out">…</p>
<script>
let age = 20;
let msg;
if (age >= 18) {
msg = "adult";
} else {
msg = "under 18";
}
let box = document.getElementById("out");
box.textContent = "status=" + msg;
</script>
</body>
</html>
age is 20, so the page shows status=adult. Set age to 16, refresh, and you should see status=under 18. The next lesson (switch) is another way to pick among many fixed values; if / else stays the everyday tool for true/false gates.
Try it yourself
- Log
"ok"insideif (true)and confirm it prints. - Use
if/elsewithlet age = 16and print"adult"or"under 18". - Build an
else ifchain forscorebands:A(≥ 90),pass(≥ 60), elseretry. - Combine
age >= 18 && score >= 60in oneifcondition. - Store a
labelstring fromif/else, then log it once. - In the HTML demo, change only
age, refresh, and read the newstatus=text.
Common mistakes
- Forgetting curly braces
{ }around the branch body - Writing
if (age = 18)with one=(assignment) instead of a comparison - Putting
elsewithout a matchingifabove it - Using many separate
ifs when you meant exclusiveelse ifpaths - Checking loose bands in the wrong order (e.g.
>= 60before>= 90)
Summary
ifruns a block when the condition is truthyelseruns when thatiffailedelse ifadds more exclusive middle paths- Conditions are usually comparisons or logical expressions
- Keep each branch inside braces; store a label when you need the result later
- Next: switch — picking among several fixed values
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top