Conditions
Why it matters
A program must be able to choose: show a "Log in" or "Log out" button, let someone into the game or say "too young", give a discount or not. That's what conditions are for — code that runs only if something is true.
How tasks with functions work
From this topic on, tasks look like this: the editor already contains a "blank" — a function:
function sign(n) {
}
A function is a named piece of code. Data comes into it through a parameter (here n), and it gives
back a result with the word return. The checker will call the function with different numbers, e.g.
sign(5) and sign(-2), and compare what it returned. Your job is to write the code inside the curly
braces. We'll cover functions in detail in topic 7; for now this is enough.
function double(n) {
return n * 2;
}
console.log(double(4));
Output:
8
if
const temperature = 30;
if (temperature > 25) {
console.log("It's hot! Take some water.");
}
console.log("Have a nice day");
Output:
It's hot! Take some water.
Have a nice day
The parentheses hold the condition. If it's true, the block in curly braces runs. If it's false,
the block is skipped and the program moves on.
else and else if
const score = 72;
if (score >= 90) {
console.log("Excellent");
} else if (score >= 70) {
console.log("Good");
} else if (score >= 50) {
console.log("Satisfactory");
} else {
console.log("Needs work");
}
Output:
Good
Conditions are checked from top to bottom, and only the first matching branch runs. Even though
72 >= 50 is true too, that check is never reached. So the order matters.
return stops the function
Inside a function, return immediately gives back the result and exits it. That's why you can skip
else after return:
function canVote(age) {
if (age >= 18) return "yes";
return "no";
}
console.log(canVote(20), canVote(15));
Output:
yes no
If a block has a single command, the curly braces can be omitted, as here. With several commands the braces are required.
Logical operators
| Operator | Meaning | Example |
|---|---|---|
&& |
AND — both conditions are true | age >= 18 && hasTicket |
|| |
OR — at least one is true | isAdmin || isOwner |
! |
NOT — flips the value | !isLoggedIn |
const age = 20;
const hasTicket = true;
console.log(age >= 18 && hasTicket); // true
console.log(age < 12 || age > 65); // false
console.log(!hasTicket); // false
You can group with parentheses: (a && b) || c.
The ternary operator ? :
A short "if-else" that returns a value:
const age = 16;
const status = age >= 18 ? "adult" : "teenager";
console.log(status);
Output:
teenager
Read it as: "condition ? value-if-yes : value-if-no". Handy for a simple choice between two
options; for complex logic, if is better.
Default values: ?? and ||
const nickname = null;
console.log(nickname ?? "Guest"); // Guest
a ?? b returns b only if a is null or undefined. a || b returns b for any falsy a,
including 0 and "". So for numbers ?? is safer: 0 ?? 10 → 0, but 0 || 10 → 10.
switch — choosing by value
const day = "sat";
switch (day) {
case "sat":
case "sun":
console.log("Weekend");
break;
case "mon":
console.log("Monday is hard");
break;
default:
console.log("Weekday");
}
Output:
Weekend
switch compares the value strictly (===) with each case. break is required — without it
execution "falls through" into the next case. default runs if nothing matched.
How it works inside
The condition in if doesn't have to be true/false — JavaScript converts it to a boolean using the
"falsy" rule: 0, "", null, undefined, NaN are false, everything else is true. So if (name)
means "if the name is not empty". And && and || are evaluated lazily: if the answer is already
clear from the first part (false && … or true || …), the second part isn't even run.
Common mistakes
=instead of===.if (x = 5)assigns 5 and is always true. Writeif (x === 5).- Wrong
else iforder. If you checkscore >= 50first, "Excellent" will never be given. Start with the strictest conditions. - A double comparison like in maths.
7 <= age <= 17works incorrectly in JS. You needage >= 7 && age <= 17. - A missing
return. A function withoutreturnreturnsundefined— the checker will show that it gotundefined. - A missing
breakinswitch. Several branches will run in a row.
Cheat sheet
| What | How |
|---|---|
| If | if (condition) { … } |
| Else if / else | else if (…) { … } else { … } |
| AND / OR / NOT | &&, ||, ! |
| Ternary | cond ? yes : no |
| Default | x ?? "fallback" |
| Choose by value | switch (x) { case …: …; break; default: … } |
| Return from a function | return value; |